home *** CD-ROM | disk | FTP | other *** search
/ APDL Eductation Resources / APDL Eductation Resources.iso / programs / electronic / rlab / !RLaB / test < prev    next >
Encoding:
Text File  |  1995-08-22  |  71.5 KB  |  3,181 lines

  1. //
  2. // Test Suite for RLaB.
  3. // This test suite is supposed to test as much as possible, and still
  4. // be runable on most platforms. This means we cannot do graphics or
  5. // other platform specific stuff like piping. However, that is OK,
  6. // since we are mostly interested in assuring the builder that RLaB
  7. // will produce correct numerical answers.
  8.  
  9. // We use randsvd, which in turn uses RLaB's random number
  10. // generator. We try to be carefull and seed the random number
  11. // generator so that each user will get similar results (or at least
  12. // similar inputs to the tests).
  13.  
  14. // Test the other style comments 
  15. 1 + 2; // A simple statement with trailing comment.
  16. #  Optional RLaB comment style.
  17. 1 + 2; #  A simple statement with trailing comment.
  18. %  Optional Matlab comment style.
  19. 1 + 2; %  A simple statement with trailing comment.
  20.  
  21. srand (SEED = 10);   // Seems to produce reasonable results
  22. rand("default");
  23.  
  24. tic();    // Start timing the tests...
  25.  
  26. //
  27. // Test Parameters and some functions we will need later
  28. //
  29.  
  30. pi = 4.0*atan(1.0);
  31. X = 3;    // should be 3 (heuristic).
  32.  
  33. //
  34. // Compute machine epsilon
  35. //
  36.  
  37. epsilon = function() 
  38. {
  39.   eps = 1.0;
  40.   while((eps + 1.0) != 1.0) 
  41.   {
  42.     eps = eps/2.0;
  43.   }
  44.   return 2*eps;
  45. };
  46.  
  47. eps = epsilon();
  48.  
  49. eye = function( m , n ) 
  50. {
  51.   if (!exist (n))
  52.   {
  53.     if(m.n != 2) { error("only 2-el MATRIX allowed as eye() arg"); }
  54.     new = zeros (m[1], m[2]);
  55.     N = min ([m[1], m[2]]);
  56.   else
  57.     if (class (m) == "string" || class (n) == "string") {
  58.       error ("eye(), string arguments not allowed");
  59.     }
  60.     if (max (size (m)) == 1 && max (size (n)) == 1)
  61.     {
  62.       new = zeros (m[1], n[1]);
  63.       N = min ([m[1], n[1]]);
  64.     else
  65.       error ("matrix arguments to eye() must be 1x1");
  66.     }
  67.   }
  68.   for(i in 1:N)
  69.   {
  70.     new[i;i] = 1.0;
  71.   }
  72.   return new;
  73. };
  74.  
  75. symm = function( A )
  76. {
  77.   return (A + A')./2;
  78. };
  79.  
  80. //-------------------------------------------------------------------//
  81.  
  82. // Synopsis:    Pascal matrix.
  83.  
  84. // Syntax:    P = pascal ( N )
  85.  
  86. // Description:
  87.  
  88. //    The Pascal matrix of order N: a symmetric positive definite
  89. //    matrix with integer entries taken from Pascal's triangle. The
  90. //    Pascal matrix is totally positive and its inverse has integer
  91. //    entries.  Its eigenvalues occur in reciprocal pairs. COND(P)
  92. //    is approximately 16^N/(N*PI) for large N. PASCAL(N,1) is the
  93. //    lower triangular Cholesky factor (up to signs of columns) of
  94. //    the Pascal matrix.   It is involutary (is its own
  95. //    inverse). PASCAL(N,2) is a transposed and permuted version of
  96. //    PASCAL(N,1) which is a cube root of the identity.
  97.  
  98. //      References:
  99. //      R. Brawer and M. Pirovino, The linear algebra of the Pascal matrix,
  100. //           Linear Algebra and Appl., 174 (1992), pp. 13-23 (this paper
  101. //           gives a factorization of L = PASCAL(N,1) and a formula for the
  102. //           elements of L^k).
  103. //      S. Karlin, Total Positivity, Volume 1, Stanford University Press,
  104. //           1968.  (Page 137: shows i+j-1 choose j is TP (i,j=0,1,...).
  105. //                   PASCAL(N) is a submatrix of this matrix.)
  106. //      M. Newman and J. Todd, The evaluation of matrix inversion programs,
  107. //           J. Soc. Indust. Appl. Math., 6(4):466--476, 1958.
  108. //      H. Rutishauser, On test matrices, Programmation en Mathematiques
  109. //           Numeriques, Editions Centre Nat. Recherche Sci., Paris, 165,
  110. //           1966, pp. 349-365.  (Gives an integral formula for the
  111. //           elements of PASCAL(N).)
  112. //      J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra,
  113. //           Birkhauser, Basel, and Academic Press, New York, 1977, p. 172.
  114. //      H.W. Turnbull, The Theory of Determinants, Matrices, and Invariants,
  115. //           Blackie, London and Glasgow, 1929.  (PASCAL(N,2) on page 332.)
  116.  
  117. //    This file is a translation of pascal.m from version 2.0 of
  118. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  119. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  120.  
  121. //-------------------------------------------------------------------//
  122.  
  123. pascal = function ( n , k )
  124. {
  125.   local(n, k)
  126.  
  127.   if (!exist (k)) { k = 0; }
  128.  
  129.   P = diag( (-1).^[0:n-1] );
  130.   P[; 1] = ones(n,1);
  131.  
  132.   //  Generate the Pascal Cholesky factor (up to signs).
  133.  
  134.   for (j in 2:n-1)
  135.   {
  136.     for (i in j+1:n)
  137.     {
  138.       P[i;j] = P[i-1;j] - P[i-1;j-1];
  139.     }
  140.   }
  141.  
  142.   if (k == 0)
  143.   {
  144.     P = P*P';
  145.   else if (k == 2) {
  146.     P = rot90(P,3);
  147.     if (n/2 == round(n/2)) { P = -P; }
  148.   }}
  149.  
  150.   return P;
  151. };
  152.  
  153. //-------------------------------------------------------------------//
  154.  
  155. // Synopsis:    Random, orthogonal upper Hessenberg matrix.
  156.  
  157. // Syntax:      H = ohess ( N )
  158.  
  159. // Description:
  160.  
  161. //      H is an N-by-N real, random, orthogonal upper Hessenberg
  162. //      matrix. Alternatively, H = OHESS(X), where X is an arbitrary
  163. //      real N-vector (N > 1) constructs H non-randomly using the
  164. //      elements of X as parameters. In both cases H is constructed
  165. //      via a product of N-1 Givens rotations. 
  166.  
  167. //      Note: See Gragg (1986) for how to represent an N-by-N
  168. //      (complex) unitary Hessenberg matrix with positive subdiagonal
  169. //      elements in terms of 2N-1 real parameters (the Schur
  170. //      parametrization). This M-file handles the real case only and
  171. //      is intended simply as a convenient way to generate random or
  172. //      non-random orthogonal Hessenberg matrices.
  173.  
  174. //      Reference:
  175. //      W.B. Gragg, The QR algorithm for unitary Hessenberg matrices,
  176. //      J. Comp. Appl. Math., 16 (1986), pp. 1-8.
  177.  
  178. //    This file is a translation of ohess.m from version 2.0 of
  179. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  180. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  181.  
  182. //-------------------------------------------------------------------//
  183.  
  184. ohess = function ( x )
  185. {
  186.   local (x)
  187.   global (pi)
  188.  
  189.   if (any (imag (x))) { error("Parameter must be real."); }
  190.  
  191.   n = max(size(x));
  192.  
  193.   if (n == 1)
  194.   {
  195.     //  Handle scalar x.
  196.     n = x;
  197.     x = rand(n-1, 1)*2*pi;
  198.     H = eye(n,n);
  199.     H[n;n] = sign(rand());
  200.   else
  201.     H = eye(n,n);
  202.     H[n;n] = sign(x[n]) + (x[n]==0);   // Second term ensures H[n;n] nonzero.
  203.   }
  204.  
  205.   for (i in n:2:-1)
  206.   {
  207.     // Apply Givens rotation through angle x(i-1).
  208.     theta = x[i-1];
  209.     c = cos(theta);
  210.     s = sin(theta);
  211.     H[ [i-1, i] ;] = [ c*H[i-1;]+s*H[i;] ;
  212.                        -s*H[i-1;]+c*H[i;] ];
  213.   }
  214.  
  215.   return H;
  216. };
  217.  
  218. //-------------------------------------------------------------------//
  219.  
  220. // Synopsis:    Random matrix with pre-assigned singular values.
  221.  
  222. // Syntax:    R = randsvd (N, KAPPA, MODE, KL, KU)
  223.  
  224. // Description:
  225.  
  226. //    R is a (banded) random matrix of order N with COND(A) = KAPPA
  227. //    and singular values from the distribution MODE.
  228.  
  229. //      N may be a 2-vector, in which case the matrix is N(1)-by-N(2).
  230. //      Available types:
  231. //             MODE = 1:   one large singular value,
  232. //             MODE = 2:   one small singular value,
  233. //             MODE = 3:   geometrically distributed singular values,
  234. //             MODE = 4:   arithmetically distributed singular values,
  235. //             MODE = 5:   random singular values with unif. dist. logarithm.
  236.  
  237. //      If omitted, MODE defaults to 3, and KAPPA defaults to SQRT(1/EPS).
  238. //      If MODE < 0 then the effect is as for ABS(MODE) except that in the
  239. //      original matrix of singular values the order of the diagonal entries
  240. //      is reversed: small to large instead of large to small.
  241. //      KL and KU are the lower and upper bandwidths respectively; if they
  242. //      are omitted a full matrix is produced.
  243. //      If only KL is present, KU defaults to KL.
  244. //      Special case: if KAPPA < 0 then a random full symmetric positive
  245. //                    definite matrix is produced with COND(A) = -KAPPA and
  246. //                    eigenvalues distributed according to MODE.
  247. //                    KL and KU, if present, are ignored.
  248.  
  249. //      This routine is similar to the more comprehensive Fortran routine xLATMS
  250. //      in the following reference:
  251. //      J.W. Demmel and A. McKenney, A test matrix generation suite,
  252. //      LAPACK Working Note #9, Courant Institute of Mathematical Sciences,
  253. //      New York, 1989.
  254.  
  255. //    This file is a translation of randsvd.m from version 2.0 of
  256. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  257. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  258.  
  259. // Dependencies
  260. #   rfile bandred
  261. #   rfile qmult
  262.  
  263. //-------------------------------------------------------------------//
  264.  
  265. randsvd = function (n, kappa, mode, kl, ku)
  266. {
  267.   local (n, kappa, mode, kl, ku)
  268.  
  269.   if (!exist (kappa)) { kappa = sqrt(1/eps); }
  270.   if (!exist (mode))  { mode = 3; }
  271.   if (!exist (kl))    { kl = n-1; }    // Full matrix.
  272.   if (!exist (ku))    { ku = kl; }     // Same upper and lower bandwidths.
  273.  
  274.   if (abs(kappa) < 1) {
  275.     error("Condition number must be at least 1!");
  276.   }
  277.  
  278.   posdef = 0;
  279.   if (kappa < 0)            // Special case.
  280.   {
  281.     posdef = 1;
  282.     kappa = -kappa;
  283.   }
  284.  
  285.   p = min(n);
  286.   m = n[1];        // Parameter n specifies dimension: m-by-n.
  287.   n = n[max(size(n))];
  288.  
  289.   if (p == 1)        // Handle case where A is a vector.
  290.   {
  291.     rand("normal", -10, 10);
  292.     A = rand(m, n);
  293.     A = A/norm(A, "2");
  294.     return A;
  295.   }
  296.  
  297.   j = abs(mode);
  298.  
  299.   // Set up vector sigma of singular values.
  300.   if (j == 3)
  301.   {
  302.     factor = kappa^(-1/(p-1));
  303.     sigma = factor.^[0:p-1];
  304.  
  305.   else if (j == 4) {
  306.     sigma = ones(p,1) - (0:p-1)'/(p-1)*(1-1/kappa);
  307.  
  308.   else if (j == 5) {    // In this case cond(A) <= kappa.
  309.     rand("uniform", 0, 1)
  310.     sigma = exp( -rand(p,1)*log(kappa) );
  311.  
  312.   else if (j == 2) {
  313.     sigma = ones(p,1);
  314.     sigma[p] = 1/kappa;
  315.  
  316.   else if (j == 1) {
  317.     sigma = ones(p,1)./kappa;
  318.     sigma[1] = 1;
  319.  
  320.   }}}}}
  321.  
  322.  
  323.   // Convert to diagonal matrix of singular values.
  324.   if (mode < 0) {
  325.     sigma = sigma[p:1:-1];
  326.   }
  327.  
  328.   sigma = diag(sigma);
  329.  
  330.   if (posdef)        // Handle special case.
  331.   {
  332.     Q = qmult(p);
  333.     A = Q'*sigma*Q;
  334.     A = (A + A')/2;    // Ensure matrix is symmetric.
  335.     return A;
  336.   }
  337.  
  338.   if (m != n) 
  339.   {
  340.     sigma[m; n] = 0;    // Expand to m-by-n diagonal matrix.
  341.   }
  342.  
  343.   if (kl == 0 && ku == 0) // Diagonal matrix requested - nothing more to do.
  344.   {
  345.     A = sigma;
  346.     return A;
  347.   }
  348.  
  349.   // A = U*sigma*V, where U, V are random orthogonal matrices from the
  350.   // Haar distribution.
  351.   A = qmult(sigma');
  352.   A = qmult(A');
  353.  
  354.   if (kl < n-1 || ku < n-1)    // Bandwidth reduction.
  355.   {
  356.    A = bandred(A, kl, ku);
  357.   }
  358.  
  359.   rand("default");
  360.   return A;
  361. };
  362.  
  363. //-------------------------------------------------------------------//
  364.  
  365. // Synopsis:    Band reduction by two-sided unitary transformations.
  366.  
  367. // Syntax:    bandred ( A , KL, KU )
  368.  
  369. // Description:
  370.  
  371. //    bandred(A, KL, KU) is a matrix unitarily equivalent to A with
  372. //    lower bandwidth KL and upper bandwidth KU (i.e. B(i,j) = 0 if
  373. //    i > j+KL or j > i+KU).  The reduction is performed using
  374. //    Householder transformations. If KU is omitted it defaults to
  375. //    KL. 
  376.  
  377. //    Called by randsvd.
  378. //    This is a `standard' reduction.  Cf. reduction to bidiagonal
  379. //    form prior to computing the SVD.  This code is a little
  380. //    wasteful in that it computes certain elements which are
  381. //    immediately set to zero! 
  382. //
  383. //      Reference:
  384. //      G.H. Golub and C.F. Van Loan, Matrix Computations, second edition,
  385. //      Johns Hopkins University Press, Baltimore, Maryland, 1989.
  386. //      Section 5.4.3.
  387.  
  388. //    This file is a translation of bandred.m from version 2.0 of
  389. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  390. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  391.  
  392. // Dependencies
  393. #   rfile house
  394.  
  395. //-------------------------------------------------------------------//
  396.  
  397. bandred = function ( A , kl , ku )
  398. {
  399.   local (A, kl, ku)
  400.  
  401.   if (!exist (ku)) { ku = kl; else ku = ku; }
  402.  
  403.   if (kl == 0 && ku == 0) {
  404.     error ("You''ve asked for a diagonal matrix.  In that case use the SVD!");
  405.   }
  406.  
  407.   // Check for special case where order of left/right transformations matters.
  408.   // Easiest approach is to work on the transpose, flipping back at the end.
  409.  
  410.   flip = 0;
  411.   if (ku == 0)
  412.   {
  413.     A = A';
  414.     temp = kl; kl = ku; ku = temp; flip = 1;
  415.   }
  416.  
  417.   m = A.nr; n = A.nc; 
  418.  
  419.   for (j in 1 : min( min(m, n), max(m-kl-1, n-ku-1) ))
  420.   {
  421.     if (j+kl+1 <= m)
  422.     {
  423.        ltmp = house(A[j+kl:m;j]);
  424.        beta = ltmp.beta; v = ltmp.v;
  425.        temp = A[j+kl:m;j:n];
  426.        A[j+kl:m;j:n] = temp - beta*v*(v'*temp);
  427.        A[j+kl+1:m;j] = zeros(m-j-kl,1);
  428.     }
  429.  
  430.     if (j+ku+1 <= n)
  431.     {
  432.        ltmp = house(A[j;j+ku:n]');
  433.        beta = ltmp.beta; v = ltmp.v;
  434.        temp = A[j:m;j+ku:n];
  435.        A[j:m;j+ku:n] = temp - beta*(temp*v)*v';
  436.        A[j;j+ku+1:n] = zeros(1,n-j-ku);
  437.     }
  438.   }
  439.  
  440.   if (flip) {
  441.     A = A';
  442.   }
  443.  
  444.   return A;
  445. };
  446.  
  447. //-------------------------------------------------------------------//
  448.  
  449. // Synopsis:    Lehmer matrix - symmetric positive definite.
  450.  
  451. // Syntax:      A = lehmer ( N )
  452.  
  453. // Description:
  454.  
  455. //      A is the symmetric positive definite N-by-N matrix with
  456. //                     A(i,j) = i/j for j >= i.
  457. //      A is totally nonnegative.  INV(A) is tridiagonal, and explicit
  458. //      formulas are known for its entries. 
  459.  
  460. //      N <= COND(A) <= 4*N*N.
  461.  
  462. //      References:
  463. //        M. Newman and J. Todd, The evaluation of matrix inversion
  464. //           programs, J. Soc. Indust. Appl. Math., 6 (1958), pp. 466-476.
  465. //        Solutions to problem E710 (proposed by D.H. Lehmer): The inverse
  466. //           of a matrix, Amer. Math. Monthly, 53 (1946), pp. 534-535.
  467. //        J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra,
  468. //           Birkhauser, Basel, and Academic Press, New York, 1977, p. 154.
  469.  
  470. //    This file is a translation of lehmer.m from version 2.0 of
  471. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  472. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  473.  
  474. //-------------------------------------------------------------------//
  475.  
  476. lehmer = function ( n )
  477. {
  478.   local (n)
  479.   global (tril)
  480.  
  481.   A = ones(n,1)*(1:n);
  482.   A = A./A';
  483.   A = tril(A) + tril(A,-1)';
  484.  
  485.   return A;
  486. };
  487.  
  488. //-------------------------------------------------------------------//
  489.  
  490. // Synopsis:    Pre-multiply by random orthogonal matrix.
  491.  
  492. // Syntax:    B = qmult ( A )
  493.  
  494. // Description:
  495.  
  496. //    B is Q*A where Q is a random real orthogonal matrix from the
  497. //    Haar distribution, of dimension the number of rows in
  498. //    A. Special case: if A is a scalar then QMULT(A) is the same as
  499.  
  500. //        qmult(eye(a))
  501.  
  502. //       Called by RANDSVD.
  503.  
  504. //       Reference:
  505. //       G.W. Stewart, The efficient generation of random
  506. //       orthogonal matrices with an application to condition estimators,
  507. //       SIAM J. Numer. Anal., 17 (1980), 403-409.
  508.  
  509. //    This file is a translation of qmult.m from version 2.0 of
  510. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  511. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  512.  
  513. //-------------------------------------------------------------------//
  514.  
  515. qmult = function ( A )
  516. {
  517.   local (A)
  518.  
  519.   n = A.nr; m = A.nc;
  520.  
  521.   //  Handle scalar A.
  522.   if (max(n,m) == 1)
  523.   {
  524.     n = A;
  525.     A = eye(n,n);
  526.   }
  527.  
  528.   d = zeros(n,n);
  529.  
  530.   for (k in n-1:1:-1)
  531.   {
  532.     // Generate random Householder transformation.
  533.     rand("normal", 0, 1);
  534.     x = rand(n-k+1,1);
  535.     s = norm(x, "2");
  536.     sgn = sign(x[1]) + (x[1]==0);    // Modification for sign(1)=1.
  537.     s = sgn*s;
  538.     d[k] = -sgn;
  539.     x[1] = x[1] + s;
  540.     beta = s*x[1];
  541.  
  542.     // Apply the transformation to A.
  543.     y = x'*A[k:n;];
  544.     A[k:n;] = A[k:n;] - x*(y/beta);
  545.   }
  546.  
  547.   // Tidy up signs.
  548.   for (i in 1:n-1)
  549.   {
  550.     A[i;] = d[i]*A[i;];
  551.   }
  552.  
  553.   A[n;] = A[n;]*sign(rand());
  554.   B = A;
  555.  
  556.   rand("default");
  557.   return B;
  558. };
  559.  
  560. //-------------------------------------------------------------------//
  561.  
  562. // Synopsis:    Create a Householder matrix.
  563.  
  564. // Syntax:    house ( X )
  565.  
  566. //    If HOUSE(x), which returns a list containing elements `v' and
  567. //    `beta',  then H = EYE - beta*v*v' is a Householder matrix such
  568. //    that Hx = -sign(x(1))*norm(x)*e_1. 
  569. //    NB: If x = 0 then v = 0, beta = 1 is returned.
  570. //            x can be real or complex.
  571. //            sign(x) := exp(i*arg(x)) ( = x./abs(x) when x ~= 0).
  572.  
  573. //    Theory: (textbook references Golub & Van Loan 1989, 38-43;
  574. //             Stewart 1973, 231-234, 262; Wilkinson 1965, 48-50).
  575. //      Hx = y: (I - beta*v*v')x = -s*e_1.
  576. //      Must have |s| = norm(x), v = x+s*e_1, and
  577. //      x'y = x'Hx =(x'Hx)' real => arg(s) = arg(x(1)).
  578. //      So take s = sign(x(1))*norm(x) (which avoids cancellation).
  579. //      v'v = (x(1)+s)^2 + x(2)^2 + ... + x(n)^2
  580. //          = 2*norm(x)*(norm(x) + |x(1)|).
  581.  
  582. //    References:
  583. //        G.H. Golub and C.F. Van Loan, Matrix Computations, second edition,
  584. //           Johns Hopkins University Press, Baltimore, Maryland, 1989.
  585. //        G.W. Stewart, Introduction to Matrix Computations, Academic Press,
  586. //           New York, 1973,
  587. //        J.H. Wilkinson, The Algebraic Eigenvalue Problem, Oxford University
  588. //           Press, 1965.
  589.  
  590. //    This file is a translation of house.m from version 2.0 of
  591. //    "The Test Matrix Toolbox for Matlab", described in Numerical
  592. //    Analysis Report No. 237, December 1993, by N. J. Higham.
  593.  
  594. //-------------------------------------------------------------------//
  595.  
  596. house = function ( x )
  597. {
  598.   local (x)
  599.  
  600.   m = x.nr; n = x.nc;
  601.   if (n > 1) { error ("Argument must be a column vector."); }
  602.  
  603.   s = norm(x,"2") * (sign(x[1]) + (x[1]==0)); // Modification for sign(0)=1.
  604.   v = x;
  605.   if (s == 0)    // Quit if x is the zero vector.
  606.   {
  607.     beta = 1; 
  608.     return << beta = beta ; v = v >>;
  609.   }
  610.  
  611.   v[1] = v[1] + s;
  612.   beta = 1/(s'*v[1]);        // NB the conjugated s.
  613.  
  614.   // beta = 1/(abs(s)*(abs(s)+abs(x(1)) would guarantee beta real.
  615.   // But beta as above can be non-real (due to rounding) only 
  616.   // when x is complex.
  617.  
  618.   return << beta = beta ; v = v >>;
  619. };
  620.  
  621. //-------------------------------------------------------------------//
  622.  
  623. //  Syntax:    tril ( A )
  624. //        tril ( A , K )
  625.  
  626. //  Description:
  627.  
  628. //  tril(x) returns the lower triangular part of A.
  629.  
  630. //  tril(A,K) returns the elements on and below the K-th diagonal of
  631. //  A.
  632.  
  633. //  K = 0: main diagonal
  634. //  K > 0: above the main diag.
  635. //  K < 0: below the main diag.
  636.  
  637. //  See Also: triu
  638. //-------------------------------------------------------------------//
  639.  
  640. tril = function(x, k) 
  641. {
  642.   local(x, k)
  643.  
  644.   if (!exist (k)) { k = 0; }
  645.   nr = x.nr; nc = x.nc;
  646.   if(k > 0) 
  647.   { 
  648.     if (k > (nc - 1)) { error ("tril: invalid value for k"); }
  649.   else
  650.     if (abs (k) > (nr - 1)) { error ("tril: invalid value for k"); }
  651.   }
  652.  
  653.   y = zeros(nr, nc);
  654.  
  655.   for(i in max( [1,1-k] ):nr) {
  656.     j = 1:min( [nc, i+k] );
  657.     y[i;j] = x[i;j];
  658.   }
  659.  
  660.   return y;
  661. };
  662.  
  663. //-------------------------------------------------------------------//
  664.  
  665. //  Syntax:    triu ( A )
  666. //        triu ( A , K )
  667.  
  668. //  Description:
  669.  
  670. //  triu(x) returns the upper triangular part of A.
  671.  
  672. //  tril(x; k) returns the elements on and above the k-th diagonal of
  673. //  A. 
  674.  
  675. //  K = 0: main diagonal
  676. //  K > 0: above the main diag.
  677. //  K < 0: below the main diag.
  678.  
  679. //  See Also: tril
  680. //-------------------------------------------------------------------//
  681.  
  682. triu = function(x, k) 
  683. {
  684.   local(x, k)
  685.  
  686.   if (!exist (k)) { k = 0; }
  687.   nr = x.nr; nc = x.nc;
  688.  
  689.   if(k > 0) 
  690.   { 
  691.     if (k > (nc - 1)) { error ("triu: invalid value for k"); }
  692.   else
  693.     if (abs (k) > (nr - 1)) { error ("triu: invalid value for k"); }
  694.   }
  695.  
  696.   y = zeros(nr, nc);
  697.  
  698.   for(j in max( [1,1+k] ):nc) {
  699.     i = 1:min( [nr, j-k] );
  700.     y[i;j] = x[i;j];
  701.   }
  702.  
  703.   return y;
  704. };
  705.  
  706. //
  707. //-------------------- Test Relational Expressions -------------------
  708. //
  709. printf("\tstart scalar tests...\n");
  710. printf("\tstart relational tests...\n");
  711.  
  712. //    SCALAR CONSTANTS (REAL)
  713. if( !(1<2) ) { error(); }
  714. if( !(1<=2) ) { error(); }
  715. if( 1>2 ) { error(); }
  716. if( 1>=2 ) { error(); }
  717. if( 1==2 ) { error(); }
  718. if( !(1!=2) ) { error(); }
  719. if( !1 ) { error(); }
  720. if( !!!1) { error(); }
  721.  
  722. if( !([1]<[2]) ) { error(); }
  723. if( !([1]<=[2]) ) { error(); }
  724. if( [1]>[2] ) { error(); }
  725. if( [1]>=[2] ) { error(); }
  726. if( [1]==[2] ) { error(); }
  727. if( !([1]!=[2]) ) { error(); }
  728. if( ![1] ) { error(); }
  729. if( !!![1]) { error(); }
  730.  
  731. //    SCALAR CONSTANTS (COMPLEX)
  732. if( !(1+2i<2+3i) ) { error(); }
  733. if( !(1+2i<=2+3i) ) { error(); }
  734. if( 1+2i>2+3i ) { error(); }
  735. if( 1+2i>=2+3i ) { error(); }
  736. if( 1+2i==2+3i ) { error(); }
  737. if( !(1+2i!=2+3i) ) { error(); }
  738. if( !1+2i ) { error(); }
  739. if( !!!1+2i) { error(); }
  740.  
  741. if( !([1+2i]<[2+3i]) ) { error(); }
  742. if( !([1+2i]<=[2+3i]) ) { error(); }
  743. if( [1+2i]>[2+3i] ) { error(); }
  744. if( [1+2i]>=[2+3i] ) { error(); }
  745. if( [1+2i]==[2+3i] ) { error(); }
  746. if( !([1+2i]!=[2+3i]) ) { error(); }
  747. if( ![1+2i] ) { error(); }
  748. if( !!![1+2i]) { error(); }
  749.  
  750. //    SCALAR ENTITIES (REAL)
  751. a=1;b=2;
  752. if( !(a<b) ) { error(); }
  753. if( !(a<=b) ) { error(); }
  754. if( a>b ) { error(); }
  755. if( a>=b ) { error(); }
  756. if( a==b ) { error(); }
  757. if( !(a!=b) ) { error(); }
  758. if( !a ) { error(); }
  759. if( !!!a) { error(); }
  760.  
  761. if( !([a]<[b]) ) { error(); }
  762. if( !([a]<=[b]) ) { error(); }
  763. if( [a]>[b] ) { error(); }
  764. if( [a]>=[b] ) { error(); }
  765. if( [a]==[b] ) { error(); }
  766. if( !([a]!=[b]) ) { error(); }
  767. if( ![a] ) { error(); }
  768. if( !!![a]) { error(); }
  769.  
  770. //    SCALAR ENTITIES (COMPLEX)
  771. a=1+2i;b=2+3i;
  772. if( !(a<b) ) { error(); }
  773. if( !(a<=b) ) { error(); }
  774. if( a>b ) { error(); }
  775. if( a>=b ) { error(); }
  776. if( a==b ) { error(); }
  777. if( !(a!=b) ) { error(); }
  778. if( !a ) { error(); }
  779. if( !!!a) { error(); }
  780.  
  781. if( !([a]<[b]) ) { error(); }
  782. if( !([a]<=[b]) ) { error(); }
  783. if( [a]>[b] ) { error(); }
  784. if( [a]>=[b] ) { error(); }
  785. if( [a]==[b] ) { error(); }
  786. if( !([a]!=[b]) ) { error(); }
  787. if( ![a] ) { error(); }
  788. if( !!![a]) { error(); }
  789.  
  790. if (! all (all (-2 <  rand(4,4)))) { error (); }
  791. if (! all (all (-2 <= rand(4,4)))) { error (); }
  792. if (! all (all ( 2 >  rand(4,4)))) { error (); }
  793. if (! all (all ( 2 >= rand(4,4)))) { error (); }
  794.  
  795. if (! all (all (rand(4,4) >  -2))) { error (); }
  796. if (! all (all (rand(4,4) >= -2))) { error (); }
  797. if (! all (all (rand(4,4) <   2))) { error (); }
  798. if (! all (all (rand(4,4) <=  2))) { error (); }
  799.  
  800. if (! all (all (rand (4,4) >  -rand (4,4)))) { error (); }
  801. if (! all (all (rand (4,4) >= -rand (4,4)))) { error (); }
  802. if (! all (all (-rand (4,4) <  rand (4,4)))) { error (); }
  803. if (! all (all (-rand (4,4) <= rand (4,4)))) { error (); }
  804.  
  805. //------------------------- Test REAL SCALARS ------------------------
  806. //
  807. //    CONSTANTS
  808. //      Addition
  809. if(1+2 != 3) { error(); }
  810. //      Subtraction
  811. if(1-2 != -1) { error(); }
  812. //      Multiply
  813. if(1*2 != 2) { error(); }
  814. //      Divide
  815. if(1/2 != 0.5) { error(); }
  816. //      Power
  817. if(2^2 != 4) { error(); }
  818. if(4^0 != 1) { error(); }
  819. //      Unary Minus
  820. if(2-3 != -1) { error(); }
  821. //
  822. //    ENTITIES
  823. //
  824. a = 1; b = 2; c = 3; d = 0.5;
  825. //      Addition
  826. if(a+b != c) { error(); }
  827. //      Subtraction
  828. if(a-b != -a) { error(); }
  829. //      Multiply
  830. if(a*b != b) { error(); }
  831. //      Divide
  832. if(a/b != d) { error(); }
  833. //      Power
  834. if(b^b != b*b) { error(); }
  835. if(b^0 != 1) { error (); }
  836. //      Unary Minus
  837. if(b-c != -a) { error(); }
  838. //
  839. //  ENTITIES & CONSTANTS
  840. //
  841. if(a+2 != c) { error(); }
  842. //      Subtraction
  843. if(1-b != -a) { error(); }
  844. //      Multiply
  845. if(a*2 != 2) { error(); }
  846. //      Divide
  847. if(1/b != d) { error(); }
  848. //      Power
  849. if(2^b != b*b) { error(); }
  850. //      Unary Minus
  851. if(b-3 != -a) { error(); }
  852. //
  853. //------------------------Test COMPLEX SCALARS -------------------------
  854. //
  855. //    CONSTANTS
  856. if(sqrt(-1) != 1i) { error(); }
  857. //      Addition
  858. if((1+2i)+(2+3i) != (3+5i)) { error(); }
  859. //      Subtraction
  860. if((1+2i)-(3+4i) != (-2-2i)) { error(); }
  861. //      Multiply
  862. if((1+2i)*(3+4i) != (-5+10i)) { error(); }
  863. //      Divide
  864. if((1+2i)/(3-4i) != (-.2+.4i)) { error(); }
  865. //      Power
  866. //      Precision problems prevent us from testing these. Have to
  867. //      be checked by hand.
  868. //  (1+2i)^2 = -3 + 4i
  869. //  (1+2i)^.5 = 1.272 + 7.862e-1i
  870. //  if((1+2i)^2 != (-3+4i)) { error(); }
  871. //  if((1+2i)^10 != (237+3116i)) { error(); }
  872. //      Unary Minus
  873. if(-(1+2i) != -1-2i) { error(); }
  874. //
  875. //    ENTITIES
  876. //
  877. a = 1+2i; b = 3+4i; c = 4+6i;
  878. if(a+b != c) { error(); }
  879. //      Subtraction
  880. if(a-b != -2-2i) { error(); }
  881. //      Multiply
  882. if(a*b != -5+10i) { error(); }
  883. //      Divide
  884. //if(a/(3-4i) != -.2+.4i) { error(); }
  885. //      Power
  886. //  if(b^b != b*b) { error(); }
  887. //      Unary Minus
  888. if(-a != -1-2i) { error(); }
  889. //
  890. //    ENTITIES & CONSTANTS
  891. //
  892. if(a+(3+4i) != c) { error(); }
  893. //      Subtraction
  894. if((1+2i)-b != -2-2i) { error(); }
  895. //      Multiply
  896. if(a*(3+4i) != -5+10i) { error(); }
  897. //      Divide
  898. //if(a/(3-4i) != -.2+.4i) { error(); }
  899. //      Power
  900. //if(b^b != b*b) { error(); }
  901. //      Unary Minus
  902. if(-(1+2i) != -1-2i) { error(); }
  903. //
  904. // String - Numerical Equalities
  905. //
  906. if ((1 == "1")) { error(); }
  907. if (([1] == "1")) { error(); }
  908. if (("1" == 1)) { error(); }
  909. if (("1" == [1])) { error(); }
  910.  
  911. if (rand(3,3) == "str") { error(); }
  912. if ("str" == rand(3,3)) { error(); }
  913. if (!any (any ((["1", "2"; "3", "4"] == "1") == [1,0;0,0]))) { error(); }
  914.  
  915. //
  916. //----------------------- Test REAL MATRICES ---------------------------
  917. //
  918.  
  919. printf("\tstart matrix tests...\n");
  920. printf("\t\treal-matrices\n");
  921.  
  922. //  Read in test matrices
  923. //
  924.  
  925. read("test_input");
  926.  
  927. //
  928. //  Matrix construction
  929. //
  930.  
  931. if(any([1;2;3] != [1,2,3]')) {
  932.   error();
  933. }
  934.  
  935. if(any (any (m0 != zeros(2,2)))) { error(); }
  936. if(any (any (m1 != 1+zeros(2,2)))) { error(); }
  937. if(any (any (m2 != [1,2;3,4]))) { error(); }
  938. if(any (any (m3 != [1+2i,2+3i;3+4i,5+6i]))) { error(); }
  939. if(any (any ([1,2;3+0i,4+0i] != m2))) { error(); }
  940. if(any (any ([m2] != m2))) { error(); }
  941.  
  942. //
  943. //  Matrix indexing
  944. //
  945.  
  946. p = pascal(6);
  947. if (!all (all (p[ [1:3] ; ] == p[ [1:3]' ; ]))) { error (); }
  948. if (!all (all (p[ ; [1:3] ] == p[ ; [1:3]' ]))) { error (); }
  949. if (!all (all (p[ [2:6] ; [2:6] ] == p[ [2:6]'; [2:6]' ]))) { error (); }
  950. if (!all (all (p[ [2:6]' ; [2:6] ] == p[ [2:6]'; [2:6]' ]))) { error (); }
  951. if (!all (all (p[ [6:12]' ] == p[ [6:12] ]))) { error (); }
  952.  
  953. //
  954. //  Sub-Matrix promotion
  955. //
  956.  
  957. if(m2[2;2] != 4) { error(); }
  958. if(any(m2[2;] != [3,4])) { error(); }
  959. if(any(m2[;2] != [2,4]')) { error(); }
  960. i=2;j=1;
  961. if(m2[i;j] != 3) { error(); }
  962. i=1;j=2;
  963. if(m2[i;j] != 2) { error(); }
  964. m = [1,2,3;4,5,6;7,8,9];
  965.  
  966. if(any(m[1;1,2] != [1,2])) 
  967. {
  968.   error();
  969. }
  970.  
  971. if(any(m[1,2;1] != [1;4])) 
  972. {
  973.   error();
  974. }
  975.  
  976. if(any (any (m[1,2;1,2] != [1,2;4,5]))) 
  977. {
  978.   error();
  979. }
  980.  
  981. //
  982.  
  983. if(m3[2;2] != (5+6i)) { error(); }
  984. if(any(m3[2;] != [3+4i,5+6i])) { error(); }
  985. if(any(m3[;2] != conj([2+3i,5+6i]'))) { error(); }
  986.  
  987. //
  988. //  Automatic creation, extension
  989. //
  990.  
  991. if(any (any ((mm[3;3]=10) != [0,0,0;0,0,0;0,0,10]))) { error(); }
  992. a=[1,2,3;4,5,6];
  993. if(any (any ((a[3;1]=10) != [1,2,3;4,5,6;10,0,0]))) { error(); }
  994. a=[1,2;3,4];
  995. if(any (any ((a[3,4;3,4]=[5,6;7,8]) != [1,2,0,0;3,4,0,0;0,0,5,6;0,0,7,8]))) 
  996. {
  997.   error();
  998. }
  999.  
  1000. mmm[2;] = a[1;];
  1001.  
  1002. //
  1003. //  Matrix binary operations
  1004. //
  1005.  
  1006. a = m2; b = [5,6;7,8];
  1007. if(any (any (a+a != [2,4;6,8]))) { error(); }
  1008. if(any (any (a-a != zeros(2,2)))) { error(); }
  1009. if(any (any (2+a != [3,4;5,6]))) { error(); }
  1010. if(any (any (2-a != [1,0;-1,-2]))) { error(); }
  1011. if(any (any (a-2 != [-1,0;1,2]))) { error(); }
  1012. if(any (any (2*a != [2,4;6,8]))) { error(); }
  1013. if(any (any ((a./2 != [0.5,1;1.5,2])))) { error(); }
  1014. if(any (any (a*a != [7,10;15,22]))) { error(); }
  1015. if(any (any (a*a*a != [37,54;81,118]))) { error(); }
  1016. if(any (any (a .* a != [1,4;9,16]))) { error(); }
  1017. if(any (any (a./a != [1,1;1,1]))) { error(); }
  1018. if(any (any (a' != [1,3;2,4]))) { error(); }
  1019.  
  1020. if(any(any(rand(3,3)^0 != eye(3,3)))) { error(); }
  1021. if(any(any(rand(3,3).^0 != ones(3,3)))) { error(); }
  1022. if(any(any(rand(1,3).^0 != ones(1,3)))) { error(); }
  1023. if(any(any(rand(3,1).^0 != ones(3,1)))) { error(); }
  1024. if(any(any(1.^zeros(3,1) != ones(3,1)))) { error(); }
  1025. if(any(any(1.^zeros(1,3) != ones(1,3)))) { error(); }
  1026.  
  1027. if(any ([1;2;3]+[4;5;6] != [5;7;9])) 
  1028. {
  1029.   error();
  1030. }
  1031.  
  1032. if(any ([1;2;3]-[4;5;6] != [-3;-3;-3])) 
  1033. {
  1034.   error();
  1035. }
  1036.  
  1037. if(any ([2;2;2] ./ [1;1;1] != [2;2;2])) 
  1038. {
  1039.   error();
  1040. }
  1041.  
  1042. if(any ([1;2;3] .* [4;5;6] != [4;10;18])) 
  1043. {
  1044.   error();
  1045. }
  1046.  
  1047. if (type (1^.33333) != "real") { error (); }
  1048. if (type (1^(1/3)) != "real") { error (); }
  1049. if (type ([1]^.33333) != "real") { error (); }
  1050. if (type (1^[.33333]) != "real") { error (); }
  1051. if (type ([1]^[.33333]) != "real") { error (); }
  1052.  
  1053. //
  1054. // Test row-wise matrix addition
  1055. //
  1056.  
  1057. a = [1,2,3]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
  1058. if (!all (all (a .+ b == [2,4,6;5,7,9;8,10,12;11,13,15]))) { error (); }
  1059. if (!all (all (b .+ a == [2,4,6;5,7,9;8,10,12;11,13,15]))) { error (); }
  1060.  
  1061. a = [1,2,3] + [1,2,3]*1i; 
  1062. b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
  1063. c = [2,4,6;5,7,9;8,10,12;11,13,15] + [2,4,6;5,7,9;8,10,12;11,13,15]*1i;
  1064. if (!all (all (a .+ b == c))) { error (); }
  1065. if (!all (all (b .+ a == c))) { error (); }
  1066.  
  1067. printf("\t\tpassed matrix row-wise add test...\n");
  1068.  
  1069. //
  1070. // Test row-wise matrix subtraction
  1071. //
  1072.  
  1073. a = [1,1,1]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
  1074. if (!all (all (a .- b == -[0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
  1075. if (!all (all (b .- a ==  [0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
  1076.  
  1077. a = [1,1,1] + [1,1,1]*1i;
  1078. b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
  1079. c = [0,1,2;3,4,5;6,7,8;9,10,11] + [0,1,2;3,4,5;6,7,8;9,10,11]*1i;
  1080. if (!all (all (a .- b == -c))) { error (); }
  1081. if (!all (all (b .- a ==  c))) { error (); }
  1082.  
  1083. printf("\t\tpassed matrix row-wise subtraction test...\n");
  1084.  
  1085. //
  1086. // Test col-wise matrix addition
  1087. //
  1088.  
  1089. a = [1;1;1;1]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
  1090. if (!all (all (a .+ b == [2,3,4;5,6,7;8,9,10;11,12,13]))) { error (); }
  1091. if (!all (all (b .+ a == [2,3,4;5,6,7;8,9,10;11,12,13]))) { error (); }
  1092.  
  1093. a = [1;1;1;1] + [1;1;1;1]*1i;
  1094. b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
  1095. c = [2,3,4;5,6,7;8,9,10;11,12,13] + [2,3,4;5,6,7;8,9,10;11,12,13]*1i;
  1096. if (!all (all (a .+ b == c))) { error (); }
  1097. if (!all (all (b .+ a == c))) { error (); }
  1098.  
  1099. printf("\t\tpassed matrix col-wise add test...\n");
  1100.  
  1101. //
  1102. // Test col-wise matrix subtraction
  1103. //
  1104.  
  1105. a = [1;1;1;1]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
  1106. if (!all (all (a .- b == -[0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
  1107. if (!all (all (b .- a ==  [0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
  1108.  
  1109. a = [1;1;1;1] + [1;1;1;1]*1i;
  1110. b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
  1111. c = [0,1,2;3,4,5;6,7,8;9,10,11] + [0,1,2;3,4,5;6,7,8;9,10,11]*1i;
  1112. if (!all (all (a .- b == -c))) { error (); }
  1113. if (!all (all (b .- a ==  c))) { error (); }
  1114.  
  1115. printf("\t\tpassed matrix col-wise subtraction test...\n");
  1116.  
  1117. a = [1,2,3];
  1118. b = [1,2,3;4,5,6;7,8,9];
  1119. c = [1,4,9;4,10,18;7,16,27];
  1120.  
  1121. if (!all (all (a .* b == c))) { error (); }
  1122. if (!all (all (b .* a == c))) { error (); }
  1123.  
  1124. za = a + rand (size (a))*1j;
  1125. zb = b + rand (size (b))*1j;
  1126.  
  1127. if (!all (all (za .* zb == [za;za;za] .* zb))) { error (); }
  1128. if (!all (all (zb .* za == zb .* [za;za;za]))) { error (); }
  1129. if (!all (all (a .* zb == [a;a;a] .* zb))) { error (); }
  1130. if (!all (all (zb .* a == zb .* [a;a;a]))) { error (); }
  1131. if (!all (all (za .* b == [za;za;za] .* b))) { error (); }
  1132. if (!all (all (b .* za == b .* [za;za;za]))) { error (); }
  1133.  
  1134. printf("\t\tpassed matrix row-wise multiplication test...\n");
  1135.  
  1136. a = [1,2,3];
  1137. b = [1,2,3;4,6,6;7,8,9];
  1138. c = [1,1,1;4,3,2;7,4,3];
  1139.  
  1140. if (!all (all (b ./ a == c))) { error (); }
  1141. if (!all (all ([a;a;a] ./ b == a ./ b))) { error (); }
  1142. if (!all (all (b ./ [a;a;a] == b ./ a))) { error (); }
  1143.  
  1144. za = a + rand (size (a))*1j;
  1145. zb = b + rand (size (b))*1j;
  1146.  
  1147. if (!all (all ([za;za;za] ./ zb == za ./ zb))) { error (); }
  1148. if (!all (all (zb ./ [za;za;za] == zb ./ za))) { error (); }
  1149. if (!all (all ([a;a;a] ./ zb == a ./ zb))) { error (); }
  1150. if (!all (all (zb ./ [a;a;a] == zb ./ a))) { error (); }
  1151. if (!all (all ([za;za;za] ./ b == za ./ b))) { error (); }
  1152. if (!all (all (b ./ [za;za;za] == b ./ za))) { error (); }
  1153.  
  1154. printf("\t\tpassed matrix row-wise division test...\n");
  1155.  
  1156. a = [1;2;3];
  1157. b = [1,2,3;4,5,6;7,8,9];
  1158.  
  1159. if (!all (all (a .* b == [a,a,a] .* b))) { error (); }
  1160. if (!all (all (b .* a == b .* [a,a,a]))) { error (); }
  1161.  
  1162. za = a + rand (size (a))*1j;
  1163. zb = b + rand (size (b))*1j;
  1164.  
  1165. if (!all (all (za .* zb == [za,za,za] .* zb))) { error (); }
  1166. if (!all (all (zb .* za == zb .* [za,za,za]))) { error (); }
  1167. if (!all (all (za .* b == [za,za,za] .* b))) { error (); }
  1168. if (!all (all (b .* za == b .* [za,za,za]))) { error (); }
  1169. if (!all (all (a .* zb == [a,a,a] .* zb))) { error (); }
  1170. if (!all (all (zb .* a == zb .* [a,a,a]))) { error (); }
  1171.  
  1172. printf("\t\tpassed matrix column-wise multiplication test...\n");
  1173.  
  1174. a = [1;2;3];
  1175. b = [1,2,3;4,6,6;7,8,9];
  1176.  
  1177. if (!all (all ([a,a,a] ./ b == a ./ b))) { error (); }
  1178. if (!all (all (b ./ [a,a,a] == b ./ a))) { error (); }
  1179.  
  1180. za = a + rand (size(a))*1j;
  1181. zb = b + rand (size(b))*1j;
  1182.  
  1183. if (!all (all ([za,za,za] ./ zb == za ./ zb))) { error (); }
  1184. if (!all (all (zb ./ [za,za,za] == zb ./ za))) { error (); }
  1185. if (!all (all ([za,za,za] ./ b == za ./ b))) { error (); }
  1186. if (!all (all (b ./ [za,za,za] == b ./ za))) { error (); }
  1187. if (!all (all ([a,a,a] ./ zb == a ./ zb))) { error (); }
  1188. if (!all (all (zb ./ [a,a,a] == zb ./ a))) { error (); }
  1189.  
  1190. printf("\t\tpassed matrix column-wise division test...\n");
  1191.  
  1192.  
  1193. //
  1194. //--------------------- Test COMPLEX MATRICES --------------------------
  1195. //
  1196. //  Automatic creation, extension
  1197. //
  1198. printf("\t\tcomplex-matrices\n");
  1199. if(any (any ((mm[3;3]=10+10i) != [0,0,0;0,0,0;0,0,10+10i]))) { error(); }
  1200. a=[1,2,3;4,5,6];
  1201. if(any (any ((a[3;1]=10+10i) != [1,2,3;4,5,6;10+10i,0,0]))) { error(); }
  1202. //
  1203. a = m3;
  1204. if(any (any (a+a != [2+4i,4+6i;6+8i,10+12i]))) { error(); }
  1205. if(any (any (a-a != zeros(2,2)))) { error(); }
  1206. if(any (any (2+a != [3+2i,4+3i;5+4i,7+6i]))) { error(); }
  1207. if(any (any (2-a != [1-2i,0-3i;-1-4i,-3-6i]))) { error(); }
  1208. if(any (any (a-2 != [-1+2i,0+3i;1+4i,3+6i]))) { error(); }
  1209. if(any (any (2*a != [2+4i,4+6i;6+8i,10+12i]))) { error(); }
  1210. if(any (any (a./2 != [.5+1i,1+1.5i;1.5+2i,2.5+3i]))) { error(); }
  1211. if(any (any (a*a != [-9+21i,-12+34i;-14+48i,-17+77i]))) { error(); }
  1212. if(any (any (a*a*a != [-223+57i,-345+113i;-469+183i,-719+337i]))) { error(); }
  1213. if(any (any (a .* a != [-3+4i,-5+12i;-7+24i,-11+60i]))) { error(); }
  1214. //
  1215. // The following test may not work on some machines
  1216. //
  1217. if(any (any (a./a != [1,1;1,1]))) { 
  1218.   printf("\t\t***complex division inaccuracy, check manually***\n");
  1219. }
  1220.  
  1221. if(any (any (a' != conj([1+2i,3+4i;2+3i,5+6i])))) { error(); }
  1222. //  
  1223. //--------------------- Test NULL MATRICES -------------------------
  1224. //
  1225. printf("\t\tnull-matrices\n");
  1226. // Create a NULL matrix
  1227. mnull = [];
  1228. // Test it with SCALARS
  1229. if( any([1,mnull] != 1)) {
  1230.   error();
  1231. }
  1232. if( any([mnull,1] != 1)) {
  1233.   error();
  1234. }
  1235. // Test with MATRIX construction
  1236. m = [1,2;3,4;5,6];
  1237. if( any([mnull;1] != [1])) {
  1238.   error();
  1239. }
  1240. if( any([1;mnull] != [1])) {
  1241.   error();
  1242. }
  1243. if( any([mnull;1,2,3] != [1,2,3])) {
  1244.   error();
  1245. }
  1246. if( any([1,2,3;mnull] != [1,2,3])) {
  1247.   error();
  1248. }
  1249. if(any (any ([mnull,m] != m))) {
  1250.   error();
  1251. }
  1252. if(any (any ([m,mnull] != m))) {
  1253.   error();
  1254. }
  1255. if(any (any ([mnull;m] != m))) {
  1256.   error();
  1257. }
  1258. if(any (any ([m;mnull] != m))) {
  1259.   error();
  1260.  
  1261. mnull = matrix();
  1262. mnull[1] = [1];
  1263. }
  1264.  
  1265. //--------------------- Test Matrix Multiply --------------------------
  1266.  
  1267. i = sqrt(-1);
  1268. a = [1,2,3;4,5,6;7,8,9];
  1269. b = [4,5,6;7,8,9;10,11,12];
  1270. c = [ 48,  54,  60 ;
  1271.      111, 126, 141 ;
  1272.      174, 198, 222 ];
  1273.  
  1274. if (any (any (c != a*b))) { error ("failed Real-Real Multiply"); }
  1275.  
  1276. az = a + b*i;
  1277. bz = b + a*i;
  1278.  
  1279. cz = [-18+141*i , -27+162*i , -36+183*i ;
  1280.         9+240*i ,   0+279*i ,  -9+318*i ;
  1281.        36+339*i ,  27+396*i ,  18+453*i ];
  1282.  
  1283. czz = [ 48+30*i ,  54+36*i  ,  60+42*i ;
  1284.        111+66*i , 126+81*i  , 141+96*i ;
  1285.        174+102*i, 198+126*i , 222+150*i ];
  1286.  
  1287. czzz = [ 48+111*i ,  54+126*i ,  60+141*i ;
  1288.         111+174*i , 126+198*i , 141+222*i ;
  1289.         174+237*i , 198+270*i , 222+303*i ];
  1290.  
  1291. if (any (any (cz != az*bz)))  { error ("failed Complex-Complex Multiply"); }
  1292. if (any (any (czz != a*bz)))  { error ("failed Real-Complex Multiply"); }
  1293. if (any (any (czzz != az*b))) { error ("failed Complex-Real Multiply"); }
  1294.  
  1295. a = [a,a];
  1296. b = [b;b];
  1297. c = [  96 , 108 , 120 ;
  1298.       222 , 252 , 282 ;
  1299.       348 , 396 , 444 ];
  1300.  
  1301. if (any (any (c != a*b))) { error ("failed Real-Real Multiply"); }
  1302.  
  1303. az = [az,az];
  1304. bz = [bz;bz];
  1305.  
  1306. cz = [  -36+282*i ,  -54+324*i ,  -72+366*i ;
  1307.          18+480*i ,    0+558*i ,  -18+636*i ;
  1308.          72+678*i ,   54+792*i ,   36+906*i ];
  1309.  
  1310. czz = [  96+60*i  , 108+72*i  , 120+84*i  ;
  1311.         222+132*i , 252+162*i , 282+192*i ;
  1312.         348+204*i , 396+252*i , 444+300*i ];
  1313.  
  1314. czzz = [  96+222*i , 108+252*i , 120+282*i ;
  1315.          222+348*i , 252+396*i , 282+444*i ;
  1316.          348+474*i , 396+540*i , 444+606*i ];
  1317.  
  1318. if (any (any (cz != az*bz)))  { error ("failed Complex-Complex Multiply"); }
  1319. if (any (any (czz != a*bz)))  { error ("failed Real-Complex Multiply"); }
  1320. if (any (any (czzz != az*b))) { error ("failed Complex-Real Multiply"); }
  1321.  
  1322. printf("\t\tpassed matrix multiply test...\n");
  1323.  
  1324. //--------------------- Test STRING MATRICES --------------------------
  1325. //
  1326. printf("\t\tstring-matrices\n");
  1327. sm = ["s1","sm2","sm3";"x1","x2","xxx3";"y1","y2","yyy3"];
  1328. if(sm[1] != "s1") { error(); }
  1329. if( sm[1;3] != "sm3" ) { error(); }
  1330. if(any(sm[2,3;3] != ["xxx3";"yyy3"]) ) { error(); }
  1331. if(any (any ((sm[1;1]="xx")!=["xx","sm2","sm3";"x1","x2","xxx3";"y1","y2","yyy3"])))
  1332. {
  1333.   error();
  1334. }
  1335. if( ((strm[1] = "strm")[1]) != "strm" ) { error(); }
  1336.  
  1337. //  Test string-matrix add functionality
  1338.  
  1339. sm = sm[1,2;1,2];
  1340. if (any (any (("1_"+sm+"_2") != ["1_xx_2","1_sm2_2";"1_x1_2","1_x2_2"]))) {error();}
  1341.  
  1342. if ("c"+["1"] != "c1") { error (); }
  1343. if (["c"]+"1" != "c1") { error (); }
  1344.  
  1345. printf("\tpassed matrix test...\n");
  1346.  
  1347. //
  1348. //---------------------------- List Tests --------------------------
  1349. //
  1350. //  List creation
  1351. listest = << << 11; 12 >>; << 21; 22>> >>;
  1352. if( listest.[1].[2] != 12 ) { error(); }
  1353. if(any(<<a=10;b=1:4;c=[1,2,3;4,5,6;7,8,9]>>.b != [1,2,3,4])) { error(); }
  1354. mlist.[0] = m;
  1355. if(any(any(mlist.[0] != m))) { error(); }
  1356.  
  1357. // Test list functions...
  1358.  
  1359. listtest.fun = function ( a ) { return sin(a); };
  1360. if (!(listtest.fun(0.5) == sin(0.5))) { error() ; }
  1361.  
  1362. // Test open-list assignment...
  1363. </v;d/> = eig(rand(3,3));
  1364.  
  1365. printf("\tpassed list test...\n");
  1366.  
  1367. //
  1368. // Reset random number generator seed...
  1369. //
  1370.  
  1371. rand("default");
  1372.  
  1373. //
  1374. //-------------------------- Test printf () --------------------------
  1375. //
  1376.  
  1377. sprintf (tmp, "%*.*d %*.*d %s %*.*f f\n", 5,3,2, 8,7,3, "string", 3, 4, 1234e-2);
  1378. if (!(tmp == "  002  0000003 string 12.3400 f\n")) { error ("sprintf() error"); }
  1379.  
  1380. sprintf (tmp, "%*.*d %*.*d %s %*.*f f\n", [5],[3],[2], [8],[7],[3], ...
  1381.          ["string"], [3], [4], [1234e-2]);
  1382. if (!(tmp == "  002  0000003 string 12.3400 f\n")) { error ("sprintf() error"); }
  1383.  
  1384. sprintf (tmp, "%*.*d %*.*d %s %*.*f f\n", [5,1],[3,2],[2,2], [8],[7],[3], ...
  1385.          ["string"], [3], [4], [1234e-2,4]);
  1386. if (!(tmp == "  002  0000003 string 12.3400 f\n")) { error ("sprintf() error"); }
  1387.  
  1388. sprintf (tmp, "%*.*d %*.*d %s %*.*f f\n", [5+2i,1],[3,2+4i],[2,2], [8],[7],[3], ...
  1389.          ["string"], [3+2i], [4], [1234e-2+12j,4]);
  1390. if (!(tmp == "  002  0000003 string 12.3400 f\n")) { error ("sprintf() error"); }
  1391.  
  1392. printf("\tpassed sprintf test...\n");
  1393.  
  1394. //
  1395. //------------------------- Test strtod()  ----------------------------
  1396. //
  1397.  
  1398. if (123.456 != strtod ("123.456")) { error (); }
  1399. if (!all (all ([1,2;3,4] == strtod (["1","2";"3","4"]))))
  1400.   { error (); }
  1401. printf("\tpassed strtod test...\n");
  1402.  
  1403. //
  1404. //------------------------- Test getline()  ---------------------------
  1405. //
  1406. //
  1407.  
  1408. close( "test_getl" );
  1409.  
  1410. x = getline( "test_getl" );
  1411. if ( x.[1] !=  123.456 ) { error(); }
  1412. if ( x.[2] != -123.456 ) { error(); }
  1413. if ( x.[3] !=  123.456 ) { error(); }
  1414.  
  1415. x = getline( "test_getl" );
  1416. if ( x.[1] !=  .123 ) { error(); }
  1417. if ( x.[2] != -.123 ) { error(); }
  1418. if ( x.[3] !=  .123 ) { error(); }
  1419.  
  1420. x = getline( "test_getl" );
  1421. if ( x.[1] !=  123 ) { error(); }
  1422. if ( x.[2] != -123 ) { error(); }
  1423. if ( x.[3] !=  123 ) { error(); }
  1424.  
  1425. x = getline( "test_getl" );
  1426. if ( x.[1] !=  1e6 ) { error(); }
  1427. if ( x.[2] != -1e6 ) { error(); }
  1428. if ( x.[3] !=  1e6 ) { error(); }
  1429.  
  1430. x = getline( "test_getl" );
  1431. if ( x.[1] !=  1e5 ) { error(); }
  1432. if ( x.[2] != -1e5 ) { error(); }
  1433. if ( x.[3] !=  1e5 ) { error(); }
  1434.  
  1435. x = getline( "test_getl" );
  1436. if ( x.[1] !=  123.456e3 ) { error(); }
  1437. if ( x.[2] != -123.456e3 ) { error(); }
  1438. if ( x.[3] !=  123.456e3 ) { error(); }
  1439.  
  1440. x = getline( "test_getl" );
  1441. if ( x.[1] !=  123.456e3 ) { error(); }
  1442. if ( x.[2] != -123.456e3 ) { error(); }
  1443. if ( x.[3] !=  123.456e3 ) { error(); }
  1444.  
  1445. x = getline( "test_getl" );
  1446. if ( x.[1] !=  123.456e-3 ) { error(); }
  1447. if ( x.[2] != -123.456e-3 ) { error(); }
  1448. if ( x.[3] !=  123.456e-3 ) { error(); }
  1449.  
  1450. x = getline( "test_getl" );
  1451. if ( x.[1] !=  .123e3 ) { error(); }
  1452. if ( x.[2] != -.123e3 ) { error(); }
  1453. if ( x.[3] !=  .123e3 ) { error(); }
  1454.  
  1455. x = getline( "test_getl" );
  1456. if ( x.[1] !=  123e3 ) { error(); }
  1457. if ( x.[2] != -123e3 ) { error(); }
  1458. if ( x.[3] !=  123e3 ) { error(); }
  1459.  
  1460. x = getline( "test_getl" );
  1461. if ( x.[1] != "123abc" ) { error(); }
  1462. if ( x.[2] != "abc123" ) { error(); }
  1463. if ( x.[3] != "123.abc" ) { error(); }
  1464.  
  1465. x = getline( "test_getl" );
  1466. if ( x.[1] != "quoted string" ) { error(); }
  1467. if ( x.[2] != "q string with escapes \n \t \" " ) { error(); }
  1468.  
  1469. x = getline( "test_getl" );
  1470. if ( x.[1] != "quoted string" ) { error(); }
  1471. if ( x.[2] !=  1.23e3 ) { error(); }
  1472. if ( x.[3] !=  100 ) { error(); }
  1473. if ( x.[4] != "q string with escapes \n \t \" " ) { error(); }
  1474. if ( x.[5] !=  200 ) { error(); }
  1475.  
  1476. x = getline ("test_getl", 0);
  1477. if (type (x) != "string") { error (); }
  1478.  
  1479. // Also check out strsplt while we are here...
  1480. if (length (strsplt(x)) != 79) { error (); }
  1481. if (length (strsplt(x, 13)) != 6) { error (); }
  1482. if (length (strsplt(x,[13,12,14,13,15,11] )) != 6) { error (); }
  1483. if (length (strsplt(x, ".")) != 7) { error (); }
  1484.  
  1485. printf("\tpassed getline() test...\n");
  1486.  
  1487. //
  1488. //---------------------- Test readb()/writeb()  --------------------
  1489. //
  1490.  
  1491. a = rand (5,5);
  1492. z = rand(3,3) + rand(3,3)*1j;
  1493. strm = what()[1:5;1:5];
  1494. pi = 4*atan(1.0);
  1495. sc = 2*pi;
  1496. str = "this is a sample string\ttab";
  1497. l = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
  1498.  
  1499. writeb ("jnk_rb", a, z, strm, sc, str, l);
  1500.  
  1501. # Set aside matrices for later tests
  1502.  
  1503. check = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
  1504.  
  1505. clear (a, z, strm, sc, str, l);
  1506.  
  1507. close ("jnk_rb");
  1508.  
  1509. readb ("jnk_rb");
  1510.  
  1511. #
  1512. # Now do checks
  1513. #
  1514.  
  1515. if (!all (all (a == check.a))) { error (); }
  1516. if (!all (all (z == check.z))) { error (); }
  1517. if (!all (all (strm == check.strm))) { error (); }
  1518. if (sc != check.sc) { error (); }
  1519. if (str != check.str) { error (); }
  1520.  
  1521. if (length (l) != 5) { error (); }
  1522. if (!all (all (l.a == check.a))) { error (); }
  1523. if (!all (all (l.z == check.z))) { error (); }
  1524. if (!all (all (l.strm == check.strm))) { error (); }
  1525. if (l.sc != check.sc) { error (); }
  1526. if (l.str != check.str) { error (); }
  1527.  
  1528.  
  1529. printf("\tpassed binary I/O test...\n");
  1530.  
  1531. //
  1532. //---------------------- Test read()/write()  --------------------
  1533. //
  1534.  
  1535. a = rand (5,5);
  1536. z = rand(3,3) + rand(3,3)*1j;
  1537. strm = what()[1:5;1:5];
  1538. pi = 4*atan(1.0);
  1539. sc = 2*pi;
  1540. str = "this is a sample string\ttab";
  1541. l = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
  1542.  
  1543. write ("jnk_ra", a, z, strm, sc, str, l);
  1544.  
  1545. # Set aside matrices for later tests
  1546.  
  1547. check = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
  1548.  
  1549. clear (a, z, strm, sc, str, l);
  1550.  
  1551. close ("jnk_ra");
  1552.  
  1553. read ("jnk_ra");
  1554.  
  1555. #
  1556. # Now do checks
  1557. #
  1558.  
  1559. if (a.nr != 5 || a.nc != 5) { error (); }
  1560. if (z.nr != 3 || z.nc != 3) { error (); }
  1561. if (strm.nr != 5 || strm.nc != 5) { error (); }
  1562. if (str != check.str) { error (); }
  1563.  
  1564. if (length (l) != 5) { error (); }
  1565. if (l.a.nr != 5 || l.a.nc != 5) { error (); }
  1566. if (l.z.nr != 3 || l.z.nc != 3) { error (); }
  1567. if (l.strm.nr != 5 || l.strm.nc != 5) { error (); }
  1568. if (l.str != check.str) { error (); }
  1569.  
  1570. printf("\tpassed ascii I/O test...\n");
  1571.  
  1572. //
  1573. //-------------------------- Test eval () ------------------------------
  1574. //
  1575.  
  1576. if (1 + 2 != eval("1 + 2")) { error ("eval() error"); }
  1577. x = function (s, a) { return eval(s); };
  1578. str = "yy = 2 + x(\"2*a\",3)";
  1579. z = eval(str);
  1580. if (z != yy) { error ("eval() error"); }
  1581. printf("\tpassed eval test...\n");
  1582.  
  1583.  
  1584. //-------------------------- ------------------------------
  1585.  
  1586. printf ("\ttest builtin function correctness...\n");
  1587.  
  1588. //-------------------------- ------------------------------
  1589.  
  1590. //
  1591. //-------------------------- Test abs () ------------------------------
  1592. //
  1593.  
  1594. A = rand(5,5);
  1595. T = ( A == abs (A));
  1596. if (!all (all (A))) { error ("abs() incorrect"); }
  1597. printf("\tabs()");
  1598.  
  1599.  
  1600. //
  1601. //-------------------------- Test max () ------------------------------
  1602. //
  1603.  
  1604. A = [1,10,100;2,20,200;1,2,3];
  1605. B = A./2;
  1606. ZA = A + rand (3,3)*A*1i;
  1607. ZB = B + rand (3,3)*B*1i;
  1608. if (!all (max (A) == [2,20,200])) { error( "max() incorrect"); }
  1609. if (max (max(A)) != 200) { error ("max() incorrect"); }
  1610. if (any (any (max (A, B) != A))) { error (); }
  1611. if (any (any (max (B, A) != max (A, B)))) { error (); }
  1612. if (any (any (max (ZB, ZA) != max (ZA, ZB)))) { error (); }
  1613. if (any (any (max (B, ZA) != max (ZA, B)))) { error (); }
  1614. if (any (any (max (ZB, A) != max (A, ZB)))) { error (); }
  1615. printf("\tmax()");
  1616.  
  1617. //
  1618. //-------------------------- Test min () ------------------------------
  1619. //
  1620.  
  1621. if (!all (min (A) == [1,2,3])) { error( "min() incorrect"); }
  1622. if (min (min(A)) != 1) { error ("min() incorrect"); }
  1623. if (any (any (min (A, B) != B))) { error (); }
  1624. if (any (any (min (B, A) != min (A, B)))) { error (); }
  1625. if (any (any (min (ZB, ZA) != min (ZA, ZB)))) { error (); }
  1626. if (any (any (min (B, ZA) != min (ZA, B)))) { error (); }
  1627. if (any (any (min (ZB, A) != min (A, ZB)))) { error (); }
  1628. printf("\tmin()");
  1629.  
  1630. //
  1631. //-------------------------- Test maxi () -----------------------------
  1632. //
  1633.  
  1634. if (!all (maxi (A) == [2,2,2])) { error( "maxi() incorrect"); }
  1635. if (maxi (maxi(A)) != 1) { error ("maxi() incorrect"); }
  1636. printf("\tmaxi()");
  1637.  
  1638. //
  1639. //-------------------------- Test mini () -----------------------------
  1640. //
  1641.  
  1642. if (!all (mini (A) == [1,3,3])) { error( "mini() incorrect"); }
  1643. if (mini (mini(A)) != 1) { error ("mini() incorrect"); }
  1644. printf("\tmini()");
  1645.  
  1646. //
  1647. //-------------------------- Test floor () ----------------------------
  1648. //
  1649.  
  1650. if (floor (1.9999) != 1) { error ("floor() output incorrect"); }
  1651. if (!all (all (floor ([1.99,1.99;2.99,2.99]) == [1,1;2,2]))) {
  1652.   error ("floor output incorrect");
  1653. }
  1654. printf("\tfloor()");
  1655.  
  1656. //
  1657. //-------------------------- Test ceil () 0----------------------------
  1658. //
  1659.  
  1660. if (ceil (1.9999) != 2) { error ("ceil() output incorrect"); }
  1661. if (!all (all (ceil ([1.99,1.99;2.99,2.99]) == [2,2;3,3]))) {
  1662.   error ("ceil output incorrect");
  1663. }
  1664. printf("\tceil()\n");
  1665.  
  1666. //
  1667. //-------------------------- Test round () ----------------------------
  1668. //
  1669.  
  1670. if (round (1.8) != 2) { error ("round() output incorrect"); }
  1671. if (round (1.4) != 1) { error ("round() output incorrect"); }
  1672. if (!all (all (round ([1.99,1.99;2.4,2.4]) == [2,2;2,2]))) {
  1673.   error ("round output incorrect");
  1674. }
  1675. printf("\tround()");
  1676.  
  1677. //
  1678. //-------------------------- Test sum () ------------------------------
  1679. //
  1680.  
  1681. S = [1:4; 4:7; 8:11];
  1682. if (sum (S[1;]) != 10) { error ("sum() incorrect"); }
  1683. if (sum (S[3;]) != 38) { error ("sum() incorrect"); }
  1684. if (!all (all (sum (S) == [13,16,19,22]))) { error ("sum() incorrect"); }
  1685. printf("\tsum()");
  1686.  
  1687.  
  1688. //
  1689. //-------------------------- Test cumsum () ------------------------------
  1690. //
  1691.  
  1692. S = [1:4; 4:7; 8:11];
  1693. if (any(cumsum (S[1;]) != [1,3,6,10])) { error ("cumsum() incorrect"); }
  1694. if (any(cumsum (S[;3]) != [3;9;19])) { error ("cumsum() incorrect"); }
  1695. if (!all (all (cumsum (S) == [1,2,3,4;5,7,9,11;13,16,19,22]))) 
  1696.   error ("cumsum() incorrect"); 
  1697. }
  1698. printf("\tcumsum()");
  1699.  
  1700. //
  1701. //-------------------------- Test prod () ------------------------------
  1702. //
  1703.  
  1704. S = [1:4; 4:7; 8:11];
  1705. if (prod (S[1;]) != 24) { error ("prod() incorrect"); }
  1706. if (prod (S[;3]) != 180) { error ("prod() incorrect"); }
  1707. if (!all (all (prod (S) == [32,90,180,308]))) 
  1708.   error ("prod() incorrect"); 
  1709. }
  1710. printf("\tprod()");
  1711.  
  1712. //
  1713. //-------------------------- Test cumprod () ------------------------------
  1714. //
  1715.  
  1716. S = [1:4; 4:7; 8:11];
  1717. if (any(cumprod (S[1;]) != [1,2,6,24])) { error ("cumprod() incorrect"); }
  1718. if (any(cumprod (S[;3]) != [3;18;180])) { error ("cumprod() incorrect"); }
  1719. if (!all (all (cumprod (S) == [1,2,3,4;4,10,18,28;32,90,180,308]))) 
  1720.   error ("cumprod() incorrect"); 
  1721. }
  1722. printf("\tcumprod()\n");
  1723.  
  1724. //
  1725. //-------------------------- Test int () ------------------------------
  1726. //
  1727.  
  1728. if (int (1.9999) != 1) { error ("int() output incorrect"); }
  1729. if (!all (all (int ([1.99,1.99;2.99,2.99]) == [1,1;2,2]))) {
  1730.   error ("int() output incorrect");
  1731. }
  1732. printf("\tint()");
  1733.  
  1734. //
  1735. //-------------------------- Test mod () ------------------------------
  1736. //
  1737.  
  1738. if (mod (1,1) != 0) { error ("mod() output incorrect"); }
  1739. if (mod (4,2) != 0) { error ("mod() output incorrect"); }
  1740. if (mod (3,2) != 1) { error ("mod() output incorrect"); }
  1741. if (mod (5,3) != 2) { error ("mod() output incorrect"); }
  1742. printf("\tmod()");
  1743.  
  1744. //
  1745. //-------------------------- Test find () ------------------------------
  1746. //
  1747.  
  1748. if (find ([0,1]) != 2) { error ("find() output incorrect"); }
  1749. if (find ([1,0]) != 1) { error ("find() output incorrect"); }
  1750. if (find ([0,1+1i]) != 2) { error ("find() output incorrect"); }
  1751. if (find ([1+0i,0]) != 1) { error ("find() output incorrect"); }
  1752. if (find ([0+1i,0]) != 1) { error ("find() output incorrect"); }
  1753.  
  1754. printf("\tfind()\n");
  1755.  
  1756.  
  1757. //-------------------------- ------------------------------
  1758.  
  1759. printf ("\ttest builtin function operation...\n");
  1760.  
  1761. //-------------------------- ------------------------------
  1762.  
  1763. //
  1764. // At least use most of the builtins....
  1765. //
  1766.  
  1767. A = rand(5,5);
  1768. Z = rand(5,5) + rand(5,5)*1j;
  1769. S = what()[3:6;2:4];
  1770.  
  1771. abs (A);
  1772. abs ([A]);
  1773. abs (3.14);
  1774.  
  1775. abs (Z);
  1776. abs ([Z]);
  1777. abs (3.14j);
  1778.  
  1779. printf ("\tabs");
  1780.  
  1781. acos (A);
  1782. acos ([A]);
  1783. acos (3.14);
  1784.  
  1785. acos (Z);
  1786. acos ([Z]);
  1787. acos (3.14j);
  1788.  
  1789. printf ("\tacos");
  1790.  
  1791. all (A);
  1792. all ([A]);
  1793. all (3.14);
  1794.  
  1795. all (Z);
  1796. all ([Z]);
  1797. all (3.14j);
  1798.  
  1799. printf ("\tall");
  1800.  
  1801. any (A);
  1802. any ([A]);
  1803. any (3.14);
  1804.  
  1805. any (Z);
  1806. any ([Z]);
  1807. any (3.14j);
  1808.  
  1809. printf ("\tany");
  1810.  
  1811. asin (A);
  1812. asin ([A]);
  1813. asin (3.14);
  1814.  
  1815. asin (Z);
  1816. asin ([Z]);
  1817. asin (3.14j);
  1818.  
  1819. printf ("\tasin");
  1820.  
  1821. atan (A);
  1822. atan ([A]);
  1823. atan (3.14);
  1824.  
  1825. atan (Z);
  1826. atan ([Z]);
  1827. atan (3.14j);
  1828.  
  1829. printf ("\tatan");
  1830.  
  1831. atan2 (A,A);
  1832. atan2 ([A],[A]);
  1833. atan2 (3.14,3.14);
  1834.  
  1835. #atan2 (Z,Z);
  1836. #atan2 ([Z],[Z]);
  1837. #atan2 (3.14j,3.14j);
  1838.  
  1839. printf ("\tatan2");
  1840. printf ("\n");
  1841.  
  1842. //cd (".");        Cannot work under riscos
  1843. printf ("\tcd");
  1844.  
  1845. ceil (A);
  1846. ceil ([A]);
  1847. ceil (3.14);
  1848.  
  1849. ceil (Z);
  1850. ceil ([Z]);
  1851. ceil (3.14j);
  1852.  
  1853. printf ("\tceil");
  1854.  
  1855. class (A);
  1856. class ([A]);
  1857. class (3.14);
  1858. class ("string");
  1859. class (S);
  1860. class ([S]);
  1861. class (<< rand(3,3); rand(4,4) >>);
  1862.  
  1863. class (Z);
  1864. class ([Z]);
  1865. class (3.14j);
  1866.  
  1867. printf ("\tclass");
  1868.  
  1869. clear (rand(3,3));
  1870. clear ([rand(3,3)]);
  1871.  
  1872. printf ("\tclear");
  1873.  
  1874. conj (A);
  1875. conj ([A]);
  1876. conj (3.14);
  1877.  
  1878. conj (Z);
  1879. conj ([Z]);
  1880. conj (3.14j);
  1881.  
  1882. printf ("\tconj");
  1883.  
  1884. cos (A);
  1885. cos ([A]);
  1886. cos (3.14);
  1887.  
  1888. cos (Z);
  1889. cos ([Z]);
  1890. cos (3.14j);
  1891.  
  1892. printf ("\tcos");
  1893.  
  1894. diag (A);
  1895. diag ([A]);
  1896. diag (3.14);
  1897.  
  1898. diag (Z);
  1899. diag ([Z]);
  1900. diag (3.14j);
  1901.  
  1902. printf ("\tdiag");
  1903. printf ("\n");
  1904.  
  1905. exist (A);
  1906. exist (Z);
  1907.  
  1908. printf ("\texist");
  1909.  
  1910. exp (A);
  1911. exp ([A]);
  1912. exp (3.14);
  1913.  
  1914. exp (Z);
  1915. exp ([Z]);
  1916. exp (3.14j);
  1917.  
  1918. printf ("\texp");
  1919.  
  1920. find (A);
  1921. find ([A]);
  1922. find (3.14);
  1923.  
  1924. find (Z);
  1925. find ([Z]);
  1926. find (3.14j);
  1927.  
  1928. printf ("\tfind");
  1929.  
  1930. floor (A);
  1931. floor ([A]);
  1932. floor (3.14);
  1933.  
  1934. floor (Z);
  1935. floor ([Z]);
  1936. floor (3.14j);
  1937.  
  1938. printf ("\tfloor");
  1939.  
  1940. format (1,1);
  1941. format ([2],[3]);
  1942. format ();
  1943. format ([3], 3);
  1944. format ();
  1945.  
  1946. printf ("\tformat");
  1947.  
  1948. imag (A);
  1949. imag ([A]);
  1950. imag (3.14);
  1951.  
  1952. imag (Z);
  1953. imag ([Z]);
  1954. imag (3.14j);
  1955.  
  1956. printf ("\timag");
  1957.  
  1958. inf ();
  1959. printf ("\tinf");
  1960. printf ("\n");
  1961.  
  1962. int (A);
  1963. int ([A]);
  1964. int (3.14);
  1965.  
  1966. int (Z);
  1967. int ([Z]);
  1968. int (3.14j);
  1969.  
  1970. printf ("\tint");
  1971.  
  1972. issymm (A);
  1973. issymm ([A]);
  1974. issymm (3.14);
  1975.  
  1976. issymm (Z);
  1977. issymm ([Z]);
  1978. issymm (3.14j);
  1979.  
  1980. printf ("\tissymm");
  1981.  
  1982. length (A);
  1983. length ([A]);
  1984. length (3.14);
  1985.  
  1986. length (Z);
  1987. length ([Z]);
  1988. length (3.14j);
  1989.  
  1990. length (3.14);
  1991. length (S);
  1992. length (<< rand(2,2); rand(10,10); 3.14 >>);
  1993.  
  1994. printf ("\tlength");
  1995.  
  1996. log (A);
  1997. log ([A]);
  1998. log (3.14);
  1999.  
  2000. log (Z);
  2001. log ([Z]);
  2002. log (3.14j);
  2003.  
  2004. printf ("\tlog");
  2005.  
  2006. log10 (A);
  2007. log10 ([A]);
  2008. log10 (3.14);
  2009.  
  2010. log10 (Z);
  2011. log10 ([Z]);
  2012. log10 (3.14j);
  2013.  
  2014. printf ("\tlog10");
  2015.  
  2016. max (A);
  2017. max ([A]);
  2018. max (3.14);
  2019.  
  2020. max (Z);
  2021. max ([Z]);
  2022. max (3.14j);
  2023.  
  2024. printf ("\tmax");
  2025.  
  2026. maxi (A);
  2027. maxi ([A]);
  2028. maxi (3.14);
  2029.  
  2030. maxi (Z);
  2031. maxi ([Z]);
  2032. maxi (3.14j);
  2033.  
  2034. printf ("\tmaxi");
  2035. printf ("\n");
  2036.  
  2037. members (<< rand(2,2); rand(); rand(3,3) >>);
  2038.  
  2039. printf ("\tmembers");
  2040.  
  2041. min (A);
  2042. min ([A]);
  2043. min (3.14);
  2044.  
  2045. min (Z);
  2046. min ([Z]);
  2047. min (3.14j);
  2048.  
  2049. printf ("\tmin");
  2050.  
  2051. mini (A);
  2052. mini ([A]);
  2053. mini (3.14);
  2054.  
  2055. mini (Z);
  2056. mini ([Z]);
  2057. mini (3.14j);
  2058.  
  2059. printf ("\tmini");
  2060.  
  2061. mod (A,A);
  2062. mod ([A],A);
  2063. mod (3.14,2);
  2064.  
  2065. mod (Z,Z);
  2066. mod ([Z],Z);
  2067. mod (3.14j,2);
  2068.  
  2069. printf ("\tmod");
  2070.  
  2071. nan ();
  2072.  
  2073. ones(3,3);
  2074. ones([4,4]);
  2075.  
  2076. printf ("\tones");
  2077.  
  2078. prod (A);
  2079. prod ([A]);
  2080. prod (3.14);
  2081.  
  2082. prod (Z);
  2083. prod ([Z]);
  2084. prod (3.14j);
  2085.  
  2086. printf ("\tprod");
  2087.  
  2088. real (A);
  2089. real ([A]);
  2090. real (3.14);
  2091.  
  2092. real (Z);
  2093. real ([Z]);
  2094. real (3.14j);
  2095.  
  2096. printf ("\treal");
  2097. printf ("\n");
  2098.  
  2099. reshape (A, A.nr*A.nc, 1);
  2100. reshape (A, [A.nr*A.nc], [1]);
  2101. reshape (Z, Z.nr*Z.nc, 1);
  2102. reshape (Z, [Z.nr*Z.nc], [1]);
  2103.  
  2104. printf ("\treshape");
  2105.  
  2106. round (A);
  2107. round ([A]);
  2108. round (3.14);
  2109.  
  2110. round (Z);
  2111. round ([Z]);
  2112. round (3.14j);
  2113.  
  2114. printf ("\tround");
  2115.  
  2116. show(A);
  2117. show(Z);
  2118. show([A]);
  2119. show([Z]);
  2120.  
  2121. printf ("\tshow");
  2122.  
  2123. sign (A);
  2124. sign ([A]);
  2125. sign (3.14);
  2126.  
  2127. sign (Z);
  2128. sign ([Z]);
  2129. sign (3.14j);
  2130.  
  2131. printf ("\tsign");
  2132.  
  2133. sin (A);
  2134. sin ([A]);
  2135. sin (3.14);
  2136.  
  2137. sin (Z);
  2138. sin ([Z]);
  2139. sin (3.14j);
  2140.  
  2141. printf ("\tsin");
  2142.  
  2143. size (A);
  2144. size ([A]);
  2145. size (3.14);
  2146.  
  2147. size (Z);
  2148. size ([Z]);
  2149. size (3.14j);
  2150.  
  2151. printf ("\tsize");
  2152.  
  2153. sizeof (A);
  2154. sizeof ([A]);
  2155. sizeof (3.14);
  2156.  
  2157. sizeof (Z);
  2158. sizeof ([Z]);
  2159. sizeof (3.14j);
  2160.  
  2161. printf ("\tsizeof");
  2162. printf ("\n");
  2163.  
  2164. sort (A);
  2165. sort ([A]);
  2166. sort (3.14);
  2167.  
  2168. sort (Z);
  2169. sort ([Z]);
  2170. sort (3.14j);
  2171.  
  2172. printf ("\tsort");
  2173.  
  2174. sqrt (A);
  2175. sqrt ([A]);
  2176. sqrt (3.14);
  2177.  
  2178. sqrt (Z);
  2179. sqrt ([Z]);
  2180. sqrt (3.14j);
  2181.  
  2182. printf ("\tsqrt");
  2183.  
  2184. srand ();
  2185. srand ("clock");
  2186. srand (SEED);
  2187.  
  2188. printf ("\tsrand");
  2189.  
  2190. strsplt (S);
  2191. printf ("\tstrsplt");
  2192.  
  2193. strtod ("string");
  2194. strtod (S);
  2195. strtod ("1.2");
  2196. strtod (["1.2", "1e3"]);
  2197.  
  2198. printf ("\tstrtod");
  2199.  
  2200. sum (A);
  2201. sum ([A]);
  2202. sum (3.14);
  2203.  
  2204. sum (Z);
  2205. sum ([Z]);
  2206. sum (3.14j);
  2207.  
  2208. printf ("\tsum");
  2209.  
  2210. tan (A);
  2211. tan ([A]);
  2212. tan (3.14);
  2213.  
  2214. tan (Z);
  2215. tan ([Z]);
  2216. tan (3.14j);
  2217.  
  2218. printf ("\ttan");
  2219. printf ("\n");
  2220.  
  2221. tmpnam ();
  2222. printf ("\ttmpnam");
  2223.  
  2224. type (A);
  2225. type ([A]);
  2226. type (Z);
  2227. type ([Z]);
  2228. type (3.14);
  2229. type (S);
  2230. type ("string");
  2231.  
  2232. printf ("\ttype");
  2233.  
  2234. zeros (3,3);
  2235. zeros ([3], [3]);
  2236. zeros ([3,3]);
  2237.  
  2238. printf ("\tzeros");
  2239.  
  2240. printf ("\n");
  2241.  
  2242. srand(SEED);
  2243. rand("default");
  2244.  
  2245. //
  2246. //-------------------------- Test rand () -----------------------------
  2247. //
  2248. rand ("normal", 5, 1);
  2249. xrand = rand(4000,1);
  2250.  
  2251. mean = function(x)
  2252. {
  2253.   local(m);
  2254.  
  2255.   m = size (x)[1];
  2256.   if( m == 1 ) 
  2257.   { 
  2258.     m = size (x)[2];
  2259.   }
  2260.  
  2261.   return sum( x ) / m;
  2262. };
  2263.  
  2264. std = function(x)
  2265. {
  2266.   local(i, m, s);
  2267.  
  2268.   if(class(x) != "num") { error("std() requires NUMERICAL input"); }
  2269.  
  2270.   m = x.nr;
  2271.   if( m == 1 ) 
  2272.   { 
  2273.     return sqrt( sum( (x - mean(x)) .^ 2 ) / (x.nc - 1) );
  2274.   else
  2275.     for( i in 1:x.nc) {
  2276.       s[i] = sqrt( sum( (x[;i] - mean(x[;i])) .^ 2 ) / (x.nr - 1) );
  2277.     }
  2278.     return s;
  2279.   }
  2280. };
  2281.  
  2282. if (!(mean (xrand) > 4.9 && mean (xrand) < 5.1)) 
  2283.   { error ("error in random"); }
  2284. if (!(std (xrand) > 0.9 && std (xrand) < 1.1))
  2285.   { error ("error in random"); }
  2286. printf("\tpassed rand test...\n");
  2287.  
  2288. rand("default");
  2289.  
  2290. //
  2291. //-------------------------- Test norm () -----------------------------
  2292. //
  2293.  
  2294. tn = [1,2,3,4;2,1,2,3;3,2,1,2;4,3,2,1  ];
  2295. if (norm(tn,"m") != 4 ) { error ("incorrect norm computation"); }
  2296. if (norm(tn,"1") != 10) { error ("incorrect norm computation"); }
  2297. if (norm(tn,"i") != 10) { error ("incorrect norm computation"); }
  2298. printf("\tpassed norm test...\n");
  2299.  
  2300. //
  2301. //-------------------------- Test qr () ------------------------------
  2302. //
  2303.  
  2304. a = ohess(4);
  2305. qa = qr (a);
  2306. if (max (max (abs (qa.q*qa.r - a)))/(X*norm (a)*a.nr) > eps)
  2307.   { error ("possible qr() problems"); }
  2308.  
  2309. z = ohess (4) + ohess(4)*1i;
  2310. qz = qr (z);
  2311. if (max (max (abs (qz.q*qz.r -  z)))/(X*norm (z)*z.nr) > eps)
  2312.   { error ("possible qr() problems"); }
  2313.  
  2314. printf("\tpassed qr test...\n");
  2315.  
  2316. //
  2317. //-------------------------- Test schur () ----------------------------
  2318. //
  2319.  
  2320. a = randsvd (10, 10);
  2321. sa = schur (a);
  2322. if (max (max (abs (sa.z*sa.t*sa.z' - a)))/(X*norm (a)*a.nr) > eps)
  2323.   { error ("possible schur() problems"); }
  2324.  
  2325. z = rand (4,4) + rand(4,4)*1i;
  2326. sz = schur (z);
  2327. if (max (max (abs (sz.z*sz.t*sz.z' - z)))/(X*norm (z)*z.nr) > eps)
  2328.   { error ("possible schur() problems"); }
  2329.  
  2330. a = randsvd (10, -10);
  2331. sa = schur (a);
  2332. if (max (max (abs (sa.z*sa.t*sa.z' - a)))/(X*norm (a)*a.nr) > eps)
  2333.   { error ("possible schur() problems"); }
  2334.  
  2335. z = rand (4,4) + rand(4,4)*1i;
  2336. sz = schur (z);
  2337. if (max (max (abs (sz.z*sz.t*sz.z' - z)))/(X*norm (z)*z.nr) > eps)
  2338.   { error ("possible schur() problems"); }
  2339.  
  2340. printf("\tpassed schur test...\n");
  2341.  
  2342. //
  2343. //-------------------------- Test schord () ----------------------------
  2344. //
  2345.  
  2346. s2a = schord (sa, 2, 4);
  2347. if (max (max (abs (s2a.z*s2a.t*s2a.z' - a)))/(X*norm (a)*a.nr) > eps)
  2348.   error ("possible schord() problems"); 
  2349. }
  2350.  
  2351. s2z = schord (sz, 3, 1);
  2352. if (max (max (abs (s2z.z*s2z.t*s2z.z' - z)))/(X*norm (z)*z.nr) > eps)
  2353.   error ("possible schord() problems"); 
  2354. }
  2355.  
  2356. printf("\tpassed schord test...\n");
  2357.  
  2358. //
  2359. //-------------------------- Test chol () -----------------------------
  2360. //
  2361.  
  2362. c = lehmer(10);
  2363. u = chol (c);
  2364. if (max (max (abs (u'*u - c)))/(X*norm (c)*c.nr) > eps)
  2365.   error ("possible chol() problems"); 
  2366. }
  2367.  
  2368. cz = lehmer(10) + lehmer(10)*1j;
  2369. cz = symm(cz);
  2370. uz = chol (cz);
  2371. if (max (max (abs (uz'*uz - cz)))/(X*norm (cz)*cz.nr) > eps)
  2372.   error ("possible chol() problems"); 
  2373. }
  2374.  
  2375. printf("\tpassed chol test...\n");
  2376.  
  2377. //
  2378. //--------------------------- Test inv () --------------------------------
  2379. //
  2380.  
  2381. a = randsvd(10,10);
  2382. b = ones(10,1);
  2383. x = inv(a) * b;
  2384. if (max (max (abs(a*x - b)))/(X*norm (a)*a.nr) > eps)
  2385.   printf ("\tThe condition # of a: %d\n", 1/rcond (a));
  2386.   printf ("\tA*X - B:\n");
  2387.   abs (a*x - b)
  2388.   error ("possible inv() problems\n");
  2389. }
  2390.  
  2391. az = randsvd(10,10) + randsvd(10,10)*1j;
  2392. bz = rand(10,1) + rand(10,1)*1j;
  2393. xz = inv (az)*bz;
  2394. if (max (max (abs (az*xz - bz)))/(X*norm (az)*az.nr) > eps)
  2395.   printf ("\tThe condition # of z: %d\n", 1/rcond (az));
  2396.   printf ("\tA*X - B:\n");
  2397.   abs (az*xz - bz)
  2398.   error ("possible inv() problems\n");
  2399. }
  2400.  
  2401. printf("\tpassed inv test...\n");
  2402.  
  2403. //
  2404. //-------------------------- Test solve () -----------------------------
  2405. //
  2406.  
  2407. //
  2408. // Real - General case
  2409. //
  2410.  
  2411. a = randsvd(10,10);
  2412. b = ones(10,1);
  2413. x = solve (a,b);
  2414. if (max (max (abs(a*x - b)))/(X*norm (a)*a.nr) > eps)
  2415.   printf ("\tThe condition # of a: %d\n", 1/rcond (a));
  2416.   printf ("\tA*X - B:\n");
  2417.   abs (a*x - b)
  2418.   error ("possible solve() problems\n");
  2419. }
  2420.  
  2421. //
  2422. // Real - Symmetric case
  2423. //
  2424.  
  2425. s = symm (randsvd(10,10));
  2426. b = ones(10,1);
  2427. x = solve (s,b);
  2428. if (max (max (abs(s*x - b)))/(X*norm (s)*s.nr) > eps)
  2429.   printf ("\tThe condition # of s: %d\n", 1/rcond (s));
  2430.   printf ("\tA*X - B:\n");
  2431.   abs (s*x - b)
  2432.   error ("possible solve() problems\n");
  2433. }
  2434.  
  2435. //
  2436. // Complex - General  case
  2437. //
  2438.  
  2439. az = randsvd(10,10) + randsvd(10,10)*1j;
  2440. bz = rand(10,1) + rand(10,1)*1j;
  2441. xz = solve (az,bz);
  2442. if (max (max (abs (az*xz - bz)))/(X*norm (az)*az.nr) > eps)
  2443.   printf ("\tThe condition # of z: %d\n", 1/rcond (az));
  2444.   printf ("\tA*X - B:\n");
  2445.   abs (az*xz - bz)
  2446.   error ("possible solve() problems\n");
  2447. }
  2448.  
  2449. //
  2450. // Complex - Symmetric case
  2451. //
  2452.  
  2453. sz = symm (randsvd(10,10) + randsvd(10,10)*1j);
  2454. bz = rand(10,1) + rand(10,1)*1j;
  2455. xz = solve (sz,bz);
  2456. if (max (max (abs (sz*xz - bz)))/(X*norm (sz)*sz.nr) > eps)
  2457.   printf ("\tThe condition # of sz: %d\n", 1/rcond (sz));
  2458.   printf ("\tA*X - B:\n");
  2459.   abs (sz*xz - bz)
  2460.   error ("possible solve() problems\n");
  2461. }
  2462.  
  2463. printf("\tpassed solve test...\n");
  2464.  
  2465. //
  2466. //-------------------------- Test factor() / backsub() -----------------------------
  2467. //
  2468.  
  2469. //
  2470. // Real - General case
  2471. //
  2472.  
  2473. a = randsvd(10,10);
  2474. b = ones(10,1);
  2475. f = factor (a);
  2476. x = backsub (f,b);
  2477. if (max (max (abs(a*x - b)))/(X*norm (a)*a.nr) > eps)
  2478.   printf ("\tThe condition # of a: %d\n", 1/rcond (a));
  2479.   printf ("\tA*X - B:\n");
  2480.   abs (a*x - b)
  2481.   error ("possible factor/backsub problems\n");
  2482. }
  2483.  
  2484. //
  2485. // Real - Symmetric case
  2486. //
  2487.  
  2488. s = symm (randsvd(10,10));
  2489. f = factor (s);
  2490. x = backsub (f,b);
  2491. if (max (max (abs(s*x - b)))/(X*norm (s)*s.nr) > eps)
  2492.   printf ("\tThe condition # of s: %d\n", 1/rcond (s));
  2493.   printf ("\tA*X - B:\n");
  2494.   abs (s*x - b)
  2495.   error ("possible factor/backsub problems\n");
  2496. }
  2497.  
  2498. s = symm (randsvd(10,10));
  2499. f = factor (s, "s");
  2500. x = backsub (f,b);
  2501. if (max (max (abs(s*x - b)))/(X*norm (s)*s.nr) > eps)
  2502.   printf ("\tThe condition # of s: %d\n", 1/rcond (s));
  2503.   printf ("\tA*X - B:\n");
  2504.   abs (s*x - b)
  2505.   error ("possible factor/backsub problems\n");
  2506. }
  2507.  
  2508. //
  2509. // Complex - General  case
  2510. //
  2511.  
  2512. az = randsvd(10,10) + randsvd(10,10)*1j;
  2513. bz = rand(10,1) + rand(10,1)*1j;
  2514. fz = factor (az);
  2515. xz = backsub (fz,bz);
  2516. if (max (max (abs (az*xz - bz)))/(X*norm (az)*az.nr) > eps)
  2517.   printf ("\tThe condition # of z: %d\n", 1/rcond (az));
  2518.   printf ("\tA*X - B:\n");
  2519.   abs (az*xz - bz)
  2520.   error ("possible factor/backsub problems\n");
  2521. }
  2522.  
  2523. az = randsvd(10,10) + randsvd(10,10)*1j;
  2524. bz = rand(10,1) + rand(10,1)*1j;
  2525. fz = factor (az, "g");
  2526. xz = backsub (fz,bz);
  2527. if (max (max (abs (az*xz - bz)))/(X*norm (az)*az.nr) > eps)
  2528.   printf ("\tThe condition # of z: %d\n", 1/rcond (az));
  2529.   printf ("\tA*X - B:\n");
  2530.   abs (az*xz - bz)
  2531.   error ("possible factor/backsub problems\n");
  2532. }
  2533.  
  2534. //
  2535. // Complex - Symmetric case
  2536. //
  2537.  
  2538. sz = symm (randsvd(10,10) + randsvd(10,10)*1j);
  2539. fz = factor(sz);
  2540. xz = backsub (fz,bz);
  2541. if (max (max (abs (sz*xz - bz)))/(X*norm (sz)*sz.nr) > eps)
  2542.   printf ("\tThe condition # of sz: %d\n", 1/rcond (sz));
  2543.   printf ("\tA*X - B:\n");
  2544.   abs (sz*xz - bz)
  2545.   error ("possible factor/backsub problems\n");
  2546. }
  2547.  
  2548. sz = symm (randsvd(10,10) + randsvd(10,10)*1j);
  2549. fz = factor(sz, "s");
  2550. xz = backsub (fz,bz);
  2551. if (max (max (abs (sz*xz - bz)))/(X*norm (sz)*sz.nr) > eps)
  2552.   printf ("\tThe condition # of sz: %d\n", 1/rcond (sz));
  2553.   printf ("\tA*X - B:\n");
  2554.   abs (sz*xz - bz)
  2555.   error ("possible factor/backsub problems\n");
  2556. }
  2557.  
  2558. printf("\tpassed factor/backsub test...\n");
  2559.  
  2560. //
  2561. //------------------------------ Test lu() ---------------------------------
  2562. //
  2563.  
  2564. static (swap);
  2565.  
  2566. lu = function ( A )
  2567. {
  2568.   local (i, l, u, pvt, x)
  2569.  
  2570.   if (A.nr != A.nc) { error ("lu() requires square A"); }
  2571.  
  2572.   x = factor (A, "g");    // Do the factorization
  2573.  
  2574.   //
  2575.   // Now create l, u, and pvt from lu and pvt.
  2576.   //
  2577.  
  2578.   l = tril (x.lu, -1) + eye (size (x.lu));
  2579.   u = triu (x.lu);
  2580.   pvt = eye (size (x.lu));
  2581.  
  2582.   //
  2583.   // Now re-arange the columns of pvt
  2584.   //
  2585.  
  2586.   for (i in 1:max (size (x.lu)))
  2587.   {
  2588.     pvt = pvt[ ; swap (1:pvt.nc, i, x.pvt[i]) ];
  2589.   }
  2590.   return << l = l; u = u; pvt = pvt >>;
  2591. };
  2592.  
  2593. //
  2594. //  In vector V, swap elements I, J
  2595. //
  2596.  
  2597. swap = function ( V, I, J )
  2598. {
  2599.   local (v, tmp);
  2600.   v = V;
  2601.   tmp = v[I];
  2602.   v[I] = v[J];
  2603.   v[J] = tmp;
  2604.   return v;
  2605. };
  2606.  
  2607. a = randsvd(10,10);
  2608. lua = lu (a);
  2609. if (max (max (abs(a - lua.pvt*lua.l*lua.u)))/(X*norm (a)*a.nr) > eps)
  2610.   printf ("\tThe condition # of a: %d\n", 1/rcond (a));
  2611.   printf ("\tA - p*l*u:\n");
  2612.   abs (a - lua.pvt*lua.l*lua.u)
  2613.   error ("possible lu()/factor() problems\n");
  2614. }
  2615.  
  2616. //
  2617. // Real
  2618. az = randsvd(10,10) + randsvd(10,10)*1j;
  2619. luz = lu (az);
  2620. if (max (max (abs (az - luz.pvt*luz.l*luz.u)))/(X*norm (az)*az.nr) > eps)
  2621.   printf ("\tThe condition # of z: %d\n", 1/rcond (az));
  2622.   printf ("\tA - p*l*u:\n");
  2623.   abs (az - luz.ovt*luz.l*luz.u)
  2624.   error ("possible lu()/factor()() problems\n");
  2625. }
  2626.  
  2627. printf("\tpassed lu/factor test...\n");
  2628.  
  2629. //
  2630. //-------------------------- Test svd ()   -----------------------------
  2631. //
  2632.  
  2633. a = randsvd(10,10);
  2634. s = svd (a);
  2635. if (max (max (abs (s.u*diag(s.sigma)*s.vt - a)))/(X*norm (a)*a.nr) > eps)
  2636. {
  2637.   error ("possible svd() problems"); 
  2638. }
  2639.  
  2640. z = randsvd(10,10) + rand(10,10)*1j;
  2641. sz = svd (z);
  2642. if (max (max (abs (sz.u*diag(sz.sigma)*sz.vt - z)))/(X*norm (z)*z.nr) > eps)
  2643.   error ("possible svd() problems"); 
  2644. }
  2645.  
  2646. printf("\tpassed svd test...\n");
  2647.  
  2648. //
  2649. //-------------------------- Test hess ()  -----------------------------
  2650. //
  2651.  
  2652. a = randsvd(10,10);
  2653. h = hess (a);
  2654. if (max (max (abs (h.p*h.h*h.p' - a)))/(X*norm (a)*a.nr) > eps)
  2655.   error ("possible hess() problems");
  2656. }
  2657.  
  2658. z = randsvd(10,10) + randsvd(10,10)*1j;
  2659. hz = hess (z);
  2660. if (max (max (abs (hz.p*hz.h*hz.p' - z)))/(X*norm (z)*z.nr) > eps)
  2661.   error ("possible hess() problems"); 
  2662. }
  2663.  
  2664. printf("\tpassed hess test...\n");
  2665.  
  2666. //
  2667. //-------------------------- Test lyap () ------------------------------
  2668. //
  2669.  
  2670. lyap = function ( A, B, C )
  2671. {
  2672.   local (A, B, C)
  2673.  
  2674.   if (!exist (B)) 
  2675.   { 
  2676.     B = A';    // Solve the special form: A*X + X*A' = -C
  2677.   }
  2678.  
  2679.   if ((A.nr != A.nc) || (B.nr != B.nc) || (C.nr != A.nr) || (C.nc != B.nr)) {
  2680.     error ("Dimensions do not agree.");
  2681.   }
  2682.  
  2683.   //
  2684.   // Schur decomposition on A and B
  2685.   //
  2686.  
  2687.   sa = schur (A);
  2688.   sb = schur (B);
  2689.  
  2690.   //
  2691.   // transform C
  2692.   //
  2693.  
  2694.   tc = sa.z' * C * sb.z;
  2695.  
  2696.   X = sylv (sa.t, sb.t, tc);
  2697.  
  2698.   //
  2699.   // Undo the transformation
  2700.   //
  2701.  
  2702.   X = sa.z * X * sb.z';
  2703.  
  2704.   return X;
  2705. };
  2706.  
  2707. a = randsvd (10,10);
  2708. b = rand (10,10);
  2709. c = rand (10,10);
  2710.  
  2711. x = lyap (a, b, c);
  2712. if (max (max (abs (a*x + x*b + c)))/(X*norm(c)*norm(a)*norm(b)) > eps)
  2713.   error ("possible problems with lyap() or sylv()"); 
  2714. }
  2715.  
  2716. printf("\tpassed lyap test...\n");
  2717.  
  2718. //
  2719. //-------------------------- Test eig () ------------------------------
  2720. //
  2721.  
  2722. trace = function(m) 
  2723. {
  2724.   local(i, tr);
  2725.  
  2726.   if(m.class != "num") { 
  2727.     error("must provide NUMERICAL input to trace()");
  2728.   }
  2729.  
  2730.   tr = 0;
  2731.   for(i in 1:min( [m.nr, m.nc] )) {
  2732.     tr = tr + m[i;i];
  2733.   }
  2734.  
  2735.   return tr;
  2736. };
  2737.  
  2738. eye = function( m , n ) 
  2739. {
  2740.   local(i, N, new);
  2741.  
  2742.   if (!exist (n))
  2743.   {
  2744.     if(m.n != 2) { error("only 2-el MATRIX allowed as eye() arg"); }
  2745.     new = zeros (m[1], m[2]);
  2746.     N = min ([m[1], m[2]]);
  2747.   else
  2748.     if (class (m) == "string" || class (n) == "string") {
  2749.       error ("eye(), string arguments not allowed");
  2750.     }
  2751.     if (max (size (m)) == 1 && max (size (n)) == 1)
  2752.     {
  2753.       new = zeros (m[1], n[1]);
  2754.       N = min ([m[1], n[1]]);
  2755.     else
  2756.       error ("matrix arguments to eye() must be 1x1");
  2757.     }
  2758.   }
  2759.   for(i in 1:N)
  2760.   {
  2761.     new[i;i] = 1.0;
  2762.   }
  2763.   return new;
  2764. };
  2765.  
  2766. //
  2767. // Standard eigenvalue problem
  2768. //
  2769.  
  2770. a = randsvd(10,10);
  2771. ta = trace (a);
  2772. sa = symm (a);
  2773. tsa = trace (sa);
  2774.  
  2775. z = randsvd(10,10) + randsvd(10,10)*1i;
  2776. tz = trace (z);
  2777. sz = symm (z);
  2778. tsz = trace (sz);
  2779.  
  2780. tol = 1.e-6;
  2781.  
  2782. if (!(ta < sum(eig(a).val) + tol && ta > sum(eig(a).val) - tol))
  2783. {
  2784.   error ("error in eig");
  2785. }
  2786. if (!(tsa < sum(eig(sa).val) + tol && tsa > sum(eig(sa).val) - tol))
  2787. {
  2788.   error ("error in eig");
  2789. }
  2790. if (abs(tz)+tol < abs(sum(eig(z).val)) && abs(tz)+tol > abs(sum(eig(z).val)))
  2791. {
  2792.   error ("error in eig");
  2793. }
  2794. if (abs(tsz)+tol < abs(sum(eig(sz).val)) && abs(tsz) > abs(sum(eig(sz).val)))
  2795. {
  2796.   error ("error in eig");
  2797. }
  2798.  
  2799. //
  2800. // Generalized eigenvalue problem
  2801. //
  2802.  
  2803. b = randsvd(10,10);
  2804. sb = symm (b) + eye(size(b))*3;
  2805. tb = trace (b);
  2806. tsb = trace (sb);
  2807.  
  2808. zb = randsvd(10,10) + randsvd(10,10)*1i;
  2809. szb = symm (zb) + eye(size(zb))*3;
  2810. tzb = trace (zb);
  2811. tszb = trace (szb);
  2812.  
  2813. eig(a,b);    // not sure of a good way to check these yet
  2814. eigs(sa,sb);
  2815. eigs(sa,sb);
  2816.  
  2817. eig(z, zb);
  2818. eigs(sz, szb);
  2819. eigs(sz, szb);
  2820.  
  2821. printf("\tpassed eig test...\n");
  2822.  
  2823. //
  2824. //-------------------------- Test fft () -----------------------------
  2825. //
  2826.  
  2827. if (100 != fft(ones(100,1))[1]) { error ("error in fft()"); }
  2828. printf("\tpassed fft test...\n");
  2829.  
  2830. //
  2831. //------------------------- Fibonacci Test -------------------------
  2832. //
  2833. //  Calculate Fibonacci numbers
  2834. //
  2835.  
  2836. i=1; 
  2837. while ( i < 2 ) { 
  2838.   i=i+1;
  2839.   a=0; b=1;
  2840.   while ( b < 10000 ) {
  2841.     c = b;
  2842.     b = a+b;
  2843.     a = c;
  2844.   }
  2845. }
  2846. if ( b != 10946 ) {
  2847.   error("failed fibonacci test");
  2848. else
  2849.   printf("\tpassed fibonacci test...\n");
  2850. }
  2851.  
  2852. //
  2853. //------------------------- Factorial Test -------------------------
  2854. //
  2855.  
  2856. fac = function(a) 
  2857. {
  2858.   if(a <= 1) 
  2859.   {
  2860.     return 1
  2861.   else
  2862.     return a*$self(a-1)
  2863.   }
  2864. };
  2865.  
  2866. if(fac(10) != 3628800)
  2867.   error(); else printf("\tpassed factorial test...\n"); 
  2868. }
  2869.  
  2870. //
  2871. //--------------------------- ACK Test ----------------------------
  2872. //
  2873.  
  2874. ack = function(a, b) 
  2875. {
  2876.   if(a == 0) { return b + 1; }
  2877.   if(b == 0) { return $self(a-1, 1); }
  2878.   return $self(a-1, $self(a, b-1));
  2879. };
  2880.  
  2881. if(ack(2,2) != 7) 
  2882. {
  2883.   error("error in ack() test");
  2884.   else
  2885.   printf("\tpassed ACK test...\n");
  2886. }
  2887.  
  2888. //
  2889. //------------------------- Prime Test -----------------------------
  2890. //
  2891. // An example that finds all primes less than limit
  2892. //
  2893.  
  2894. primes = function (limit) 
  2895. {
  2896.   local(prime, cnt, i, j, k);
  2897.   
  2898.   i = 1; cnt = 0;
  2899.   for(k in 2:limit) 
  2900.   {
  2901.     j = 2;
  2902.     while(mod(k,j) != 0) 
  2903.     {
  2904.       j++;
  2905.     }
  2906.     if(j == k)             // Found prime
  2907.     {
  2908.       cnt++;
  2909.       prime[i;1] = k;
  2910.       i++;
  2911.     }
  2912.   }
  2913.   return prime;
  2914. };
  2915.  
  2916. if(max(size(primes(100))) != 25) 
  2917. {  
  2918.   error("error in prime test");
  2919.   else
  2920.   printf("\tpassed prime test...\n");
  2921. }
  2922.  
  2923. //
  2924. //--------------------------- Fib Min Test -----------------------------
  2925. //
  2926. //    fibmin() will minimize an arbitrary function 
  2927. //    in 1D using Fibonacci search
  2928.  
  2929. f065 = function(x)
  2930. {
  2931.   return (x - 0.65) * (x - 0.65);
  2932. };
  2933.  
  2934. fib = function(x)
  2935. {
  2936.   local (n, a, b);
  2937.   
  2938.   a = 1;
  2939.   b = 1;
  2940.   if (x >= 2)
  2941.   {
  2942.     n = x - 1;
  2943.     for (n in n:1:-1)
  2944.     {
  2945.       c = b;
  2946.       b = a + b;
  2947.       a = c;
  2948.       n = n - 1;
  2949.     }
  2950.   }
  2951.   return b;
  2952. };
  2953.  
  2954. //  Minimize a 1D function using Fibonacci search
  2955. //  f = function to minimize
  2956. //  xlo = lower bound
  2957. //  xhi = upper bound
  2958. //  n = number of iterations (the bigger the more accurate)
  2959.  
  2960. fibmin = function(f, xlo, xhi, n) 
  2961. {
  2962.   local(a, b, x, y, ex, ey, k, lo, hi);
  2963.   
  2964.   lo = xlo;
  2965.   hi = xhi;
  2966.   k = n;
  2967.   for (k in k:2:-1)
  2968.   {
  2969.     a = fib(k - 2) / fib(k);
  2970.     b = fib(k - 1) / fib(k);
  2971.     x = lo + (hi - lo) * a;
  2972.     y = lo + (hi - lo) * b;
  2973.     ex = f(x);
  2974.     ey = f(y);
  2975.     if (ex >= ey)
  2976.     {
  2977.       lo = x;
  2978.       else
  2979.       hi = y;
  2980.     }
  2981.     //  printf("%d: (%g %g) %g %g %g %g\n",  k, a, b, lo, hi, ex, ey);
  2982.   }
  2983.   return (lo + hi) / 2;
  2984. };
  2985.  
  2986. //
  2987. // Simple example using above function to mimize f065. Answer is 0.65
  2988. //
  2989.  
  2990. x = fibmin(f065, 0, 1, 30); // printf("f(%g)=%g\n", x, f065(x));
  2991. if (abs(x - 0.65) > 1e-6)
  2992. {
  2993.   printf("x = %f\n", x);
  2994.   error("failed fibmin test");
  2995. }
  2996.  
  2997. printf("\tpassed fibmin test...\n");
  2998.  
  2999. //
  3000. //--------------------- Nasty Function Test ------------------------
  3001. //
  3002.  
  3003. printf("\tStarting Nasty Function Test...");
  3004. printf("\tthis will take awhile\n");
  3005. check = function( a, b, c, d, e, f, g, h ) {
  3006.   if ( a+b+c+d == e+f+g+h && ...
  3007.        a^2+b^2+c^2+d^2 == e^2+f^2+g^2+h^2 && ...
  3008.        a^3+b^3+c^3+d^3 == e^3+f^3+g^3+h^3 ) {
  3009.     return 1;
  3010.   else
  3011.     return 0;
  3012.   }
  3013. };
  3014.  
  3015. for(a in 8:10) {
  3016.   for(b in 7:(a-1)) {
  3017.     for(c in 6:(b-1)) {
  3018.       for(d in 5:(c-1)) {
  3019.         for(e in 4:(d-1)) {
  3020.           for(f in 3:(e-1)) {
  3021.             for(g in 2:(f-1)) {
  3022.               for(h in 1:(g-1)) {                  
  3023.               if(check( a, b, c, d,  e, f, g, h ) || ...
  3024.                      check( a, e, c, d,  b, f, g, h ) || ...
  3025.                      check( a, f, c, d,  e, b, g, h ) || ...
  3026.                      check( a, g, c, d,  e, f, b, h ) || ...
  3027.                      check( a, h, c, d,  e, f, g, b ) || ...
  3028.                      check( a, b, e, d,  c, f, g, h ) || ...
  3029.                      check( a, b, f, d,  e, c, g, h ) || ...
  3030.                      check( a, b, g, d,  e, f, c, h ) || ...
  3031.                      check( a, b, h, d,  e, f, g, c ) || ...
  3032.                      check( a, b, c, e,  d, f, g, h ) || ...
  3033.                      check( a, b, c, f,  e, d, g, h ) || ...
  3034.                      check( a, b, c, g,  e, f, d, h ) || ...
  3035.                      check( a, b, c, h,  e, f, g, d ) || ...
  3036.                      check( a, e, f, d,  b, c, g, h ) || ...
  3037.                      check( a, e, g, d,  b, f, c, h ) || ...
  3038.                      check( a, e, h, d,  b, f, g, c ) || ...
  3039.                      check( a, f, g, d,  e, b, c, h ) || ...
  3040.                      check( a, f, h, d,  e, b, g, c ) || ...
  3041.                      check( a, g, h, d,  e, f, b, c ) || ...
  3042.                      check( a, b, e, f,  c, d, g, h ) || ...
  3043.                      check( a, b, e, g,  c, f, d, h ) || ...
  3044.                      check( a, b, e, h,  c, f, g, d ) || ...
  3045.                      check( a, b, f, g,  e, c, d, h ) || ...
  3046.                      check( a, b, f, h,  e, c, g, d ) || ...
  3047.                      check( a, b, g, h,  e, f, c, d ) || ...
  3048.                      check( a, e, f, g,  e, f, g, h ) || ...
  3049.                      check( a, e, f, h,  e, f, g, h ) || ...
  3050.                      check( a, e, g, h,  e, f, g, h ) || ...
  3051.                      check( a, f, g, h,  e, f, g, h ) ) { cnt++; }
  3052.               }
  3053.             }
  3054.           }
  3055.         }
  3056.       }
  3057.     }
  3058.   }
  3059. }
  3060.  
  3061. if(1) {  // figure out the value of cnt, and check!
  3062.   printf("\tpassed nasty function test...\n");
  3063. else
  3064.   error();
  3065. }
  3066.  
  3067. //
  3068. //------------------ Test More Advanced Functions --------------------
  3069. //
  3070.  
  3071. printf( "\tStarting the lqr/ode test..." );
  3072. printf( "\tthis will take awhile\n" );
  3073.  
  3074. lqr = function( a, b, q, r ) 
  3075. {
  3076.   local( k, s,...
  3077.          m, n, mb, nb, mq, nq,...
  3078.          e, v, d );
  3079.  
  3080.   m = size(a)[1]; n = size(a)[2];
  3081.   mb = size(b)[1]; nb = size(b)[2];
  3082.   mq = size(q)[1]; nq = size(q)[2];
  3083.   
  3084.   if ( m != mq || n != nq ) 
  3085.   {
  3086.     fprintf( "stderr", "A and Q must be the same size.\n" );
  3087.     quit
  3088.   }
  3089.     
  3090.   mr = size(r)[1]; nr = size(r)[2];
  3091.   if ( mr != nr || nb != mr ) 
  3092.   {
  3093.     fprintf( "stderr", "B and R must be consistent.\n" );
  3094.     quit
  3095.   }
  3096.     
  3097.   nn = zeros( m, nb );
  3098.     
  3099.   // Start eigenvector decomposition by finding eigenvectors of Hamiltonian:
  3100.     
  3101.   e = eig( [ a, solve(r',b')'*b'; q, -a' ] );
  3102.   v = e.vec; d = e.val;
  3103.     
  3104.   index = sort( real( d ) ).ind;
  3105.   d = real( d[ index ] );
  3106.   
  3107.   if ( !( d[n] < 0 && d[n+1] > 0 ) ) 
  3108.   {
  3109.     fprintf( "stderr", "Can't order eigenvalues.\n" );
  3110.     quit
  3111.   }
  3112.   
  3113.   chi = v[ 1:n; index[1:n] ];
  3114.   lambda = v[ (n+1):(2*n); index[1:n] ];
  3115.   s = -real(solve(chi',lambda')');
  3116.   k = solve( r, nn'+b'*s );
  3117.   
  3118.   return << k=k; s=s >>;
  3119.   
  3120. };
  3121.  
  3122. // Now run a little test problem.
  3123.  
  3124. k = 1; m = 1; c = .1;
  3125. a = [0     ,1    ,0    , 0;
  3126.     -k/m, -c/m,  k/m,  c/m;
  3127.      0,     0,    0,    1;
  3128.      k/m,  c/m, -k/m, -c/m ];
  3129. b = [ 0; 1/m; 0; 0 ];
  3130. qxx = diag( [0, 0, 100, 0] );
  3131. ruu = [1];
  3132. K = lqr( a, b, qxx, ruu ).k;
  3133.  
  3134. dot = function( t, x ) 
  3135. {
  3136.   global (a, b, K)
  3137.   return (a-b*K)*x + b*K*([1,0,1,0]');
  3138. };
  3139.  
  3140. x = ode ( dot, 0, 15, [0,0,0,0], .02, 1e-5, 1e-5 );
  3141.  
  3142. m = maxi( x[;2] );
  3143.  
  3144. if ( (abs( x[m;2] - 1.195 ) > 0.001)  || ...
  3145.      any (abs( x[x.nr;2,4] - 1 ) > 0.001) ) 
  3146. {
  3147.   printf( "\tfailed***\n" );
  3148.   else
  3149.   printf( "\tpassed the lqr/ode test...\n" );
  3150. }
  3151.  
  3152. printf("Elapsed time = %10.3f seconds\n", toc() );
  3153. "FINISHED TESTS"
  3154.